smb2file.c 7.8 KB

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