smb2file.c 7.6 KB

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