smb2file.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * fs/cifs/smb2file.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002, 2011
  5. * Author(s): Steve French (sfrench@us.ibm.com),
  6. * Pavel Shilovsky ((pshilovsky@samba.org) 2012
  7. *
  8. * This library is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published
  10. * by the Free Software Foundation; either version 2.1 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  16. * the GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/fs.h>
  23. #include <linux/stat.h>
  24. #include <linux/slab.h>
  25. #include <linux/pagemap.h>
  26. #include <asm/div64.h>
  27. #include "cifsfs.h"
  28. #include "cifspdu.h"
  29. #include "cifsglob.h"
  30. #include "cifsproto.h"
  31. #include "cifs_debug.h"
  32. #include "cifs_fs_sb.h"
  33. #include "cifs_unicode.h"
  34. #include "fscache.h"
  35. #include "smb2proto.h"
  36. int
  37. smb2_open_file(const unsigned int xid, struct cifs_open_parms *oparms,
  38. __u32 *oplock, FILE_ALL_INFO *buf)
  39. {
  40. int rc;
  41. __le16 *smb2_path;
  42. struct smb2_file_all_info *smb2_data = NULL;
  43. __u8 smb2_oplock;
  44. struct cifs_fid *fid = oparms->fid;
  45. struct network_resiliency_req nr_ioctl_req;
  46. smb2_path = cifs_convert_path_to_utf16(oparms->path, oparms->cifs_sb);
  47. if (smb2_path == NULL) {
  48. rc = -ENOMEM;
  49. goto out;
  50. }
  51. smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
  52. GFP_KERNEL);
  53. if (smb2_data == NULL) {
  54. rc = -ENOMEM;
  55. goto out;
  56. }
  57. oparms->desired_access |= FILE_READ_ATTRIBUTES;
  58. smb2_oplock = SMB2_OPLOCK_LEVEL_BATCH;
  59. rc = SMB2_open(xid, oparms, smb2_path, &smb2_oplock, smb2_data, NULL,
  60. NULL, NULL);
  61. if (rc)
  62. goto out;
  63. if (oparms->tcon->use_resilient) {
  64. /* default timeout is 0, servers pick default (120 seconds) */
  65. nr_ioctl_req.Timeout =
  66. cpu_to_le32(oparms->tcon->handle_timeout);
  67. nr_ioctl_req.Reserved = 0;
  68. rc = SMB2_ioctl(xid, oparms->tcon, fid->persistent_fid,
  69. fid->volatile_fid, FSCTL_LMR_REQUEST_RESILIENCY,
  70. true /* is_fsctl */,
  71. (char *)&nr_ioctl_req, sizeof(nr_ioctl_req),
  72. CIFSMaxBufSize, NULL, NULL /* no return info */);
  73. if (rc == -EOPNOTSUPP) {
  74. cifs_dbg(VFS,
  75. "resiliency not supported by server, disabling\n");
  76. oparms->tcon->use_resilient = false;
  77. } else if (rc)
  78. cifs_dbg(FYI, "error %d setting resiliency\n", rc);
  79. rc = 0;
  80. }
  81. if (buf) {
  82. /* if open response does not have IndexNumber field - get it */
  83. if (smb2_data->IndexNumber == 0) {
  84. rc = SMB2_get_srv_num(xid, oparms->tcon,
  85. fid->persistent_fid,
  86. fid->volatile_fid,
  87. &smb2_data->IndexNumber);
  88. if (rc) {
  89. /*
  90. * let get_inode_info disable server inode
  91. * numbers
  92. */
  93. smb2_data->IndexNumber = 0;
  94. rc = 0;
  95. }
  96. }
  97. move_smb2_info_to_cifs(buf, smb2_data);
  98. }
  99. *oplock = smb2_oplock;
  100. out:
  101. kfree(smb2_data);
  102. kfree(smb2_path);
  103. return rc;
  104. }
  105. int
  106. smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
  107. const unsigned int xid)
  108. {
  109. int rc = 0, stored_rc;
  110. unsigned int max_num, num = 0, max_buf;
  111. struct smb2_lock_element *buf, *cur;
  112. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  113. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  114. struct cifsLockInfo *li, *tmp;
  115. __u64 length = 1 + flock->fl_end - flock->fl_start;
  116. struct list_head tmp_llist;
  117. INIT_LIST_HEAD(&tmp_llist);
  118. /*
  119. * Accessing maxBuf is racy with cifs_reconnect - need to store value
  120. * and check it before using.
  121. */
  122. max_buf = tcon->ses->server->maxBuf;
  123. if (max_buf < sizeof(struct smb2_lock_element))
  124. return -EINVAL;
  125. BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);
  126. max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);
  127. max_num = max_buf / sizeof(struct smb2_lock_element);
  128. buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
  129. if (!buf)
  130. return -ENOMEM;
  131. cur = buf;
  132. cifs_down_write(&cinode->lock_sem);
  133. list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
  134. if (flock->fl_start > li->offset ||
  135. (flock->fl_start + length) <
  136. (li->offset + li->length))
  137. continue;
  138. if (current->tgid != li->pid)
  139. /*
  140. * flock and OFD lock are associated with an open
  141. * file description, not the process.
  142. */
  143. if (!(flock->fl_flags & (FL_FLOCK | FL_OFDLCK)))
  144. continue;
  145. if (cinode->can_cache_brlcks) {
  146. /*
  147. * We can cache brlock requests - simply remove a lock
  148. * from the file's list.
  149. */
  150. list_del(&li->llist);
  151. cifs_del_lock_waiters(li);
  152. kfree(li);
  153. continue;
  154. }
  155. cur->Length = cpu_to_le64(li->length);
  156. cur->Offset = cpu_to_le64(li->offset);
  157. cur->Flags = cpu_to_le32(SMB2_LOCKFLAG_UNLOCK);
  158. /*
  159. * We need to save a lock here to let us add it again to the
  160. * file's list if the unlock range request fails on the server.
  161. */
  162. list_move(&li->llist, &tmp_llist);
  163. if (++num == max_num) {
  164. stored_rc = smb2_lockv(xid, tcon,
  165. cfile->fid.persistent_fid,
  166. cfile->fid.volatile_fid,
  167. current->tgid, num, buf);
  168. if (stored_rc) {
  169. /*
  170. * We failed on the unlock range request - add
  171. * all locks from the tmp list to the head of
  172. * the file's list.
  173. */
  174. cifs_move_llist(&tmp_llist,
  175. &cfile->llist->locks);
  176. rc = stored_rc;
  177. } else
  178. /*
  179. * The unlock range request succeed - free the
  180. * tmp list.
  181. */
  182. cifs_free_llist(&tmp_llist);
  183. cur = buf;
  184. num = 0;
  185. } else
  186. cur++;
  187. }
  188. if (num) {
  189. stored_rc = smb2_lockv(xid, tcon, cfile->fid.persistent_fid,
  190. cfile->fid.volatile_fid, current->tgid,
  191. num, buf);
  192. if (stored_rc) {
  193. cifs_move_llist(&tmp_llist, &cfile->llist->locks);
  194. rc = stored_rc;
  195. } else
  196. cifs_free_llist(&tmp_llist);
  197. }
  198. up_write(&cinode->lock_sem);
  199. kfree(buf);
  200. return rc;
  201. }
  202. static int
  203. smb2_push_mand_fdlocks(struct cifs_fid_locks *fdlocks, const unsigned int xid,
  204. struct smb2_lock_element *buf, unsigned int max_num)
  205. {
  206. int rc = 0, stored_rc;
  207. struct cifsFileInfo *cfile = fdlocks->cfile;
  208. struct cifsLockInfo *li;
  209. unsigned int num = 0;
  210. struct smb2_lock_element *cur = buf;
  211. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  212. list_for_each_entry(li, &fdlocks->locks, llist) {
  213. cur->Length = cpu_to_le64(li->length);
  214. cur->Offset = cpu_to_le64(li->offset);
  215. cur->Flags = cpu_to_le32(li->type |
  216. SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
  217. if (++num == max_num) {
  218. stored_rc = smb2_lockv(xid, tcon,
  219. cfile->fid.persistent_fid,
  220. cfile->fid.volatile_fid,
  221. current->tgid, num, buf);
  222. if (stored_rc)
  223. rc = stored_rc;
  224. cur = buf;
  225. num = 0;
  226. } else
  227. cur++;
  228. }
  229. if (num) {
  230. stored_rc = smb2_lockv(xid, tcon,
  231. cfile->fid.persistent_fid,
  232. cfile->fid.volatile_fid,
  233. current->tgid, num, buf);
  234. if (stored_rc)
  235. rc = stored_rc;
  236. }
  237. return rc;
  238. }
  239. int
  240. smb2_push_mandatory_locks(struct cifsFileInfo *cfile)
  241. {
  242. int rc = 0, stored_rc;
  243. unsigned int xid;
  244. unsigned int max_num, max_buf;
  245. struct smb2_lock_element *buf;
  246. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  247. struct cifs_fid_locks *fdlocks;
  248. xid = get_xid();
  249. /*
  250. * Accessing maxBuf is racy with cifs_reconnect - need to store value
  251. * and check it for zero before using.
  252. */
  253. max_buf = tlink_tcon(cfile->tlink)->ses->server->maxBuf;
  254. if (max_buf < sizeof(struct smb2_lock_element)) {
  255. free_xid(xid);
  256. return -EINVAL;
  257. }
  258. BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);
  259. max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);
  260. max_num = max_buf / sizeof(struct smb2_lock_element);
  261. buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
  262. if (!buf) {
  263. free_xid(xid);
  264. return -ENOMEM;
  265. }
  266. list_for_each_entry(fdlocks, &cinode->llist, llist) {
  267. stored_rc = smb2_push_mand_fdlocks(fdlocks, xid, buf, max_num);
  268. if (stored_rc)
  269. rc = stored_rc;
  270. }
  271. kfree(buf);
  272. free_xid(xid);
  273. return rc;
  274. }