smb2file.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. void
  37. smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
  38. {
  39. oplock &= 0xFF;
  40. if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
  41. return;
  42. if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
  43. oplock == SMB2_OPLOCK_LEVEL_BATCH) {
  44. cinode->clientCanCacheAll = true;
  45. cinode->clientCanCacheRead = true;
  46. cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
  47. &cinode->vfs_inode);
  48. } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
  49. cinode->clientCanCacheAll = false;
  50. cinode->clientCanCacheRead = true;
  51. cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
  52. &cinode->vfs_inode);
  53. } else {
  54. cinode->clientCanCacheAll = false;
  55. cinode->clientCanCacheRead = false;
  56. }
  57. }
  58. int
  59. smb2_open_file(const unsigned int xid, struct cifs_open_parms *oparms,
  60. __u32 *oplock, FILE_ALL_INFO *buf)
  61. {
  62. int rc;
  63. __le16 *smb2_path;
  64. struct smb2_file_all_info *smb2_data = NULL;
  65. __u8 smb2_oplock[17];
  66. struct cifs_fid *fid = oparms->fid;
  67. smb2_path = cifs_convert_path_to_utf16(oparms->path, oparms->cifs_sb);
  68. if (smb2_path == NULL) {
  69. rc = -ENOMEM;
  70. goto out;
  71. }
  72. smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  73. GFP_KERNEL);
  74. if (smb2_data == NULL) {
  75. rc = -ENOMEM;
  76. goto out;
  77. }
  78. oparms->desired_access |= FILE_READ_ATTRIBUTES;
  79. *smb2_oplock = SMB2_OPLOCK_LEVEL_BATCH;
  80. if (oparms->tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
  81. memcpy(smb2_oplock + 1, fid->lease_key, SMB2_LEASE_KEY_SIZE);
  82. rc = SMB2_open(xid, oparms, smb2_path, smb2_oplock, smb2_data);
  83. if (rc)
  84. goto out;
  85. if (buf) {
  86. /* open response does not have IndexNumber field - get it */
  87. rc = SMB2_get_srv_num(xid, oparms->tcon, fid->persistent_fid,
  88. fid->volatile_fid,
  89. &smb2_data->IndexNumber);
  90. if (rc) {
  91. /* let get_inode_info disable server inode numbers */
  92. smb2_data->IndexNumber = 0;
  93. rc = 0;
  94. }
  95. move_smb2_info_to_cifs(buf, smb2_data);
  96. }
  97. *oplock = *smb2_oplock;
  98. out:
  99. kfree(smb2_data);
  100. kfree(smb2_path);
  101. return rc;
  102. }
  103. int
  104. smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
  105. const unsigned int xid)
  106. {
  107. int rc = 0, stored_rc;
  108. unsigned int max_num, num = 0, max_buf;
  109. struct smb2_lock_element *buf, *cur;
  110. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  111. struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
  112. struct cifsLockInfo *li, *tmp;
  113. __u64 length = 1 + flock->fl_end - flock->fl_start;
  114. struct list_head tmp_llist;
  115. INIT_LIST_HEAD(&tmp_llist);
  116. /*
  117. * Accessing maxBuf is racy with cifs_reconnect - need to store value
  118. * and check it for zero before using.
  119. */
  120. max_buf = tcon->ses->server->maxBuf;
  121. if (!max_buf)
  122. return -EINVAL;
  123. max_num = max_buf / sizeof(struct smb2_lock_element);
  124. buf = kzalloc(max_num * sizeof(struct smb2_lock_element), GFP_KERNEL);
  125. if (!buf)
  126. return -ENOMEM;
  127. cur = buf;
  128. down_write(&cinode->lock_sem);
  129. list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
  130. if (flock->fl_start > li->offset ||
  131. (flock->fl_start + length) <
  132. (li->offset + li->length))
  133. continue;
  134. if (current->tgid != li->pid)
  135. continue;
  136. if (cinode->can_cache_brlcks) {
  137. /*
  138. * We can cache brlock requests - simply remove a lock
  139. * from the file's list.
  140. */
  141. list_del(&li->llist);
  142. cifs_del_lock_waiters(li);
  143. kfree(li);
  144. continue;
  145. }
  146. cur->Length = cpu_to_le64(li->length);
  147. cur->Offset = cpu_to_le64(li->offset);
  148. cur->Flags = cpu_to_le32(SMB2_LOCKFLAG_UNLOCK);
  149. /*
  150. * We need to save a lock here to let us add it again to the
  151. * file's list if the unlock range request fails on the server.
  152. */
  153. list_move(&li->llist, &tmp_llist);
  154. if (++num == max_num) {
  155. stored_rc = smb2_lockv(xid, tcon,
  156. cfile->fid.persistent_fid,
  157. cfile->fid.volatile_fid,
  158. current->tgid, num, buf);
  159. if (stored_rc) {
  160. /*
  161. * We failed on the unlock range request - add
  162. * all locks from the tmp list to the head of
  163. * the file's list.
  164. */
  165. cifs_move_llist(&tmp_llist,
  166. &cfile->llist->locks);
  167. rc = stored_rc;
  168. } else
  169. /*
  170. * The unlock range request succeed - free the
  171. * tmp list.
  172. */
  173. cifs_free_llist(&tmp_llist);
  174. cur = buf;
  175. num = 0;
  176. } else
  177. cur++;
  178. }
  179. if (num) {
  180. stored_rc = smb2_lockv(xid, tcon, cfile->fid.persistent_fid,
  181. cfile->fid.volatile_fid, current->tgid,
  182. num, buf);
  183. if (stored_rc) {
  184. cifs_move_llist(&tmp_llist, &cfile->llist->locks);
  185. rc = stored_rc;
  186. } else
  187. cifs_free_llist(&tmp_llist);
  188. }
  189. up_write(&cinode->lock_sem);
  190. kfree(buf);
  191. return rc;
  192. }
  193. static int
  194. smb2_push_mand_fdlocks(struct cifs_fid_locks *fdlocks, const unsigned int xid,
  195. struct smb2_lock_element *buf, unsigned int max_num)
  196. {
  197. int rc = 0, stored_rc;
  198. struct cifsFileInfo *cfile = fdlocks->cfile;
  199. struct cifsLockInfo *li;
  200. unsigned int num = 0;
  201. struct smb2_lock_element *cur = buf;
  202. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  203. list_for_each_entry(li, &fdlocks->locks, llist) {
  204. cur->Length = cpu_to_le64(li->length);
  205. cur->Offset = cpu_to_le64(li->offset);
  206. cur->Flags = cpu_to_le32(li->type |
  207. SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
  208. if (++num == max_num) {
  209. stored_rc = smb2_lockv(xid, tcon,
  210. cfile->fid.persistent_fid,
  211. cfile->fid.volatile_fid,
  212. current->tgid, num, buf);
  213. if (stored_rc)
  214. rc = stored_rc;
  215. cur = buf;
  216. num = 0;
  217. } else
  218. cur++;
  219. }
  220. if (num) {
  221. stored_rc = smb2_lockv(xid, tcon,
  222. cfile->fid.persistent_fid,
  223. cfile->fid.volatile_fid,
  224. current->tgid, num, buf);
  225. if (stored_rc)
  226. rc = stored_rc;
  227. }
  228. return rc;
  229. }
  230. int
  231. smb2_push_mandatory_locks(struct cifsFileInfo *cfile)
  232. {
  233. int rc = 0, stored_rc;
  234. unsigned int xid;
  235. unsigned int max_num, max_buf;
  236. struct smb2_lock_element *buf;
  237. struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
  238. struct cifs_fid_locks *fdlocks;
  239. xid = get_xid();
  240. /*
  241. * Accessing maxBuf is racy with cifs_reconnect - need to store value
  242. * and check it for zero before using.
  243. */
  244. max_buf = tlink_tcon(cfile->tlink)->ses->server->maxBuf;
  245. if (!max_buf) {
  246. free_xid(xid);
  247. return -EINVAL;
  248. }
  249. max_num = max_buf / sizeof(struct smb2_lock_element);
  250. buf = kzalloc(max_num * sizeof(struct smb2_lock_element), GFP_KERNEL);
  251. if (!buf) {
  252. free_xid(xid);
  253. return -ENOMEM;
  254. }
  255. list_for_each_entry(fdlocks, &cinode->llist, llist) {
  256. stored_rc = smb2_push_mand_fdlocks(fdlocks, xid, buf, max_num);
  257. if (stored_rc)
  258. rc = stored_rc;
  259. }
  260. kfree(buf);
  261. free_xid(xid);
  262. return rc;
  263. }