smb2file.c 7.7 KB

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