misc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * fs/cifs/misc.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2008
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/slab.h>
  22. #include <linux/ctype.h>
  23. #include <linux/mempool.h>
  24. #include "cifspdu.h"
  25. #include "cifsglob.h"
  26. #include "cifsproto.h"
  27. #include "cifs_debug.h"
  28. #include "smberr.h"
  29. #include "nterr.h"
  30. #include "cifs_unicode.h"
  31. #ifdef CONFIG_CIFS_SMB2
  32. #include "smb2pdu.h"
  33. #endif
  34. extern mempool_t *cifs_sm_req_poolp;
  35. extern mempool_t *cifs_req_poolp;
  36. /* The xid serves as a useful identifier for each incoming vfs request,
  37. in a similar way to the mid which is useful to track each sent smb,
  38. and CurrentXid can also provide a running counter (although it
  39. will eventually wrap past zero) of the total vfs operations handled
  40. since the cifs fs was mounted */
  41. unsigned int
  42. _get_xid(void)
  43. {
  44. unsigned int xid;
  45. spin_lock(&GlobalMid_Lock);
  46. GlobalTotalActiveXid++;
  47. /* keep high water mark for number of simultaneous ops in filesystem */
  48. if (GlobalTotalActiveXid > GlobalMaxActiveXid)
  49. GlobalMaxActiveXid = GlobalTotalActiveXid;
  50. if (GlobalTotalActiveXid > 65000)
  51. cifs_dbg(FYI, "warning: more than 65000 requests active\n");
  52. xid = GlobalCurrentXid++;
  53. spin_unlock(&GlobalMid_Lock);
  54. return xid;
  55. }
  56. void
  57. _free_xid(unsigned int xid)
  58. {
  59. spin_lock(&GlobalMid_Lock);
  60. /* if (GlobalTotalActiveXid == 0)
  61. BUG(); */
  62. GlobalTotalActiveXid--;
  63. spin_unlock(&GlobalMid_Lock);
  64. }
  65. struct cifs_ses *
  66. sesInfoAlloc(void)
  67. {
  68. struct cifs_ses *ret_buf;
  69. ret_buf = kzalloc(sizeof(struct cifs_ses), GFP_KERNEL);
  70. if (ret_buf) {
  71. atomic_inc(&sesInfoAllocCount);
  72. ret_buf->status = CifsNew;
  73. ++ret_buf->ses_count;
  74. INIT_LIST_HEAD(&ret_buf->smb_ses_list);
  75. INIT_LIST_HEAD(&ret_buf->tcon_list);
  76. mutex_init(&ret_buf->session_mutex);
  77. }
  78. return ret_buf;
  79. }
  80. void
  81. sesInfoFree(struct cifs_ses *buf_to_free)
  82. {
  83. if (buf_to_free == NULL) {
  84. cifs_dbg(FYI, "Null buffer passed to sesInfoFree\n");
  85. return;
  86. }
  87. atomic_dec(&sesInfoAllocCount);
  88. kfree(buf_to_free->serverOS);
  89. kfree(buf_to_free->serverDomain);
  90. kfree(buf_to_free->serverNOS);
  91. if (buf_to_free->password) {
  92. memset(buf_to_free->password, 0, strlen(buf_to_free->password));
  93. kfree(buf_to_free->password);
  94. }
  95. kfree(buf_to_free->user_name);
  96. kfree(buf_to_free->domainName);
  97. kfree(buf_to_free->auth_key.response);
  98. kfree(buf_to_free);
  99. }
  100. struct cifs_tcon *
  101. tconInfoAlloc(void)
  102. {
  103. struct cifs_tcon *ret_buf;
  104. ret_buf = kzalloc(sizeof(struct cifs_tcon), GFP_KERNEL);
  105. if (ret_buf) {
  106. atomic_inc(&tconInfoAllocCount);
  107. ret_buf->tidStatus = CifsNew;
  108. ++ret_buf->tc_count;
  109. INIT_LIST_HEAD(&ret_buf->openFileList);
  110. INIT_LIST_HEAD(&ret_buf->tcon_list);
  111. spin_lock_init(&ret_buf->open_file_lock);
  112. #ifdef CONFIG_CIFS_STATS
  113. spin_lock_init(&ret_buf->stat_lock);
  114. #endif
  115. }
  116. return ret_buf;
  117. }
  118. void
  119. tconInfoFree(struct cifs_tcon *buf_to_free)
  120. {
  121. if (buf_to_free == NULL) {
  122. cifs_dbg(FYI, "Null buffer passed to tconInfoFree\n");
  123. return;
  124. }
  125. atomic_dec(&tconInfoAllocCount);
  126. kfree(buf_to_free->nativeFileSystem);
  127. if (buf_to_free->password) {
  128. memset(buf_to_free->password, 0, strlen(buf_to_free->password));
  129. kfree(buf_to_free->password);
  130. }
  131. kfree(buf_to_free);
  132. }
  133. struct smb_hdr *
  134. cifs_buf_get(void)
  135. {
  136. struct smb_hdr *ret_buf = NULL;
  137. size_t buf_size = sizeof(struct smb_hdr);
  138. #ifdef CONFIG_CIFS_SMB2
  139. /*
  140. * SMB2 header is bigger than CIFS one - no problems to clean some
  141. * more bytes for CIFS.
  142. */
  143. buf_size = sizeof(struct smb2_hdr);
  144. #endif
  145. /*
  146. * We could use negotiated size instead of max_msgsize -
  147. * but it may be more efficient to always alloc same size
  148. * albeit slightly larger than necessary and maxbuffersize
  149. * defaults to this and can not be bigger.
  150. */
  151. ret_buf = mempool_alloc(cifs_req_poolp, GFP_NOFS);
  152. /* clear the first few header bytes */
  153. /* for most paths, more is cleared in header_assemble */
  154. if (ret_buf) {
  155. memset(ret_buf, 0, buf_size + 3);
  156. atomic_inc(&bufAllocCount);
  157. #ifdef CONFIG_CIFS_STATS2
  158. atomic_inc(&totBufAllocCount);
  159. #endif /* CONFIG_CIFS_STATS2 */
  160. }
  161. return ret_buf;
  162. }
  163. void
  164. cifs_buf_release(void *buf_to_free)
  165. {
  166. if (buf_to_free == NULL) {
  167. /* cifs_dbg(FYI, "Null buffer passed to cifs_buf_release\n");*/
  168. return;
  169. }
  170. mempool_free(buf_to_free, cifs_req_poolp);
  171. atomic_dec(&bufAllocCount);
  172. return;
  173. }
  174. struct smb_hdr *
  175. cifs_small_buf_get(void)
  176. {
  177. struct smb_hdr *ret_buf = NULL;
  178. /* We could use negotiated size instead of max_msgsize -
  179. but it may be more efficient to always alloc same size
  180. albeit slightly larger than necessary and maxbuffersize
  181. defaults to this and can not be bigger */
  182. ret_buf = mempool_alloc(cifs_sm_req_poolp, GFP_NOFS);
  183. if (ret_buf) {
  184. /* No need to clear memory here, cleared in header assemble */
  185. /* memset(ret_buf, 0, sizeof(struct smb_hdr) + 27);*/
  186. atomic_inc(&smBufAllocCount);
  187. #ifdef CONFIG_CIFS_STATS2
  188. atomic_inc(&totSmBufAllocCount);
  189. #endif /* CONFIG_CIFS_STATS2 */
  190. }
  191. return ret_buf;
  192. }
  193. void
  194. cifs_small_buf_release(void *buf_to_free)
  195. {
  196. if (buf_to_free == NULL) {
  197. cifs_dbg(FYI, "Null buffer passed to cifs_small_buf_release\n");
  198. return;
  199. }
  200. mempool_free(buf_to_free, cifs_sm_req_poolp);
  201. atomic_dec(&smBufAllocCount);
  202. return;
  203. }
  204. void
  205. free_rsp_buf(int resp_buftype, void *rsp)
  206. {
  207. if (resp_buftype == CIFS_SMALL_BUFFER)
  208. cifs_small_buf_release(rsp);
  209. else if (resp_buftype == CIFS_LARGE_BUFFER)
  210. cifs_buf_release(rsp);
  211. }
  212. /* NB: MID can not be set if treeCon not passed in, in that
  213. case it is responsbility of caller to set the mid */
  214. void
  215. header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
  216. const struct cifs_tcon *treeCon, int word_count
  217. /* length of fixed section (word count) in two byte units */)
  218. {
  219. char *temp = (char *) buffer;
  220. memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
  221. buffer->smb_buf_length = cpu_to_be32(
  222. (2 * word_count) + sizeof(struct smb_hdr) -
  223. 4 /* RFC 1001 length field does not count */ +
  224. 2 /* for bcc field itself */) ;
  225. buffer->Protocol[0] = 0xFF;
  226. buffer->Protocol[1] = 'S';
  227. buffer->Protocol[2] = 'M';
  228. buffer->Protocol[3] = 'B';
  229. buffer->Command = smb_command;
  230. buffer->Flags = 0x00; /* case sensitive */
  231. buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
  232. buffer->Pid = cpu_to_le16((__u16)current->tgid);
  233. buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
  234. if (treeCon) {
  235. buffer->Tid = treeCon->tid;
  236. if (treeCon->ses) {
  237. if (treeCon->ses->capabilities & CAP_UNICODE)
  238. buffer->Flags2 |= SMBFLG2_UNICODE;
  239. if (treeCon->ses->capabilities & CAP_STATUS32)
  240. buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  241. /* Uid is not converted */
  242. buffer->Uid = treeCon->ses->Suid;
  243. buffer->Mid = get_next_mid(treeCon->ses->server);
  244. }
  245. if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
  246. buffer->Flags2 |= SMBFLG2_DFS;
  247. if (treeCon->nocase)
  248. buffer->Flags |= SMBFLG_CASELESS;
  249. if ((treeCon->ses) && (treeCon->ses->server))
  250. if (treeCon->ses->server->sign)
  251. buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  252. }
  253. /* endian conversion of flags is now done just before sending */
  254. buffer->WordCount = (char) word_count;
  255. return;
  256. }
  257. static int
  258. check_smb_hdr(struct smb_hdr *smb)
  259. {
  260. /* does it have the right SMB "signature" ? */
  261. if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff)) {
  262. cifs_dbg(VFS, "Bad protocol string signature header 0x%x\n",
  263. *(unsigned int *)smb->Protocol);
  264. return 1;
  265. }
  266. /* if it's a response then accept */
  267. if (smb->Flags & SMBFLG_RESPONSE)
  268. return 0;
  269. /* only one valid case where server sends us request */
  270. if (smb->Command == SMB_COM_LOCKING_ANDX)
  271. return 0;
  272. cifs_dbg(VFS, "Server sent request, not response. mid=%u\n",
  273. get_mid(smb));
  274. return 1;
  275. }
  276. int
  277. checkSMB(char *buf, unsigned int total_read, struct TCP_Server_Info *server)
  278. {
  279. struct smb_hdr *smb = (struct smb_hdr *)buf;
  280. __u32 rfclen = be32_to_cpu(smb->smb_buf_length);
  281. __u32 clc_len; /* calculated length */
  282. cifs_dbg(FYI, "checkSMB Length: 0x%x, smb_buf_length: 0x%x\n",
  283. total_read, rfclen);
  284. /* is this frame too small to even get to a BCC? */
  285. if (total_read < 2 + sizeof(struct smb_hdr)) {
  286. if ((total_read >= sizeof(struct smb_hdr) - 1)
  287. && (smb->Status.CifsError != 0)) {
  288. /* it's an error return */
  289. smb->WordCount = 0;
  290. /* some error cases do not return wct and bcc */
  291. return 0;
  292. } else if ((total_read == sizeof(struct smb_hdr) + 1) &&
  293. (smb->WordCount == 0)) {
  294. char *tmp = (char *)smb;
  295. /* Need to work around a bug in two servers here */
  296. /* First, check if the part of bcc they sent was zero */
  297. if (tmp[sizeof(struct smb_hdr)] == 0) {
  298. /* some servers return only half of bcc
  299. * on simple responses (wct, bcc both zero)
  300. * in particular have seen this on
  301. * ulogoffX and FindClose. This leaves
  302. * one byte of bcc potentially unitialized
  303. */
  304. /* zero rest of bcc */
  305. tmp[sizeof(struct smb_hdr)+1] = 0;
  306. return 0;
  307. }
  308. cifs_dbg(VFS, "rcvd invalid byte count (bcc)\n");
  309. } else {
  310. cifs_dbg(VFS, "Length less than smb header size\n");
  311. }
  312. return -EIO;
  313. }
  314. /* otherwise, there is enough to get to the BCC */
  315. if (check_smb_hdr(smb))
  316. return -EIO;
  317. clc_len = smbCalcSize(smb);
  318. if (4 + rfclen != total_read) {
  319. cifs_dbg(VFS, "Length read does not match RFC1001 length %d\n",
  320. rfclen);
  321. return -EIO;
  322. }
  323. if (4 + rfclen != clc_len) {
  324. __u16 mid = get_mid(smb);
  325. /* check if bcc wrapped around for large read responses */
  326. if ((rfclen > 64 * 1024) && (rfclen > clc_len)) {
  327. /* check if lengths match mod 64K */
  328. if (((4 + rfclen) & 0xFFFF) == (clc_len & 0xFFFF))
  329. return 0; /* bcc wrapped */
  330. }
  331. cifs_dbg(FYI, "Calculated size %u vs length %u mismatch for mid=%u\n",
  332. clc_len, 4 + rfclen, mid);
  333. if (4 + rfclen < clc_len) {
  334. cifs_dbg(VFS, "RFC1001 size %u smaller than SMB for mid=%u\n",
  335. rfclen, mid);
  336. return -EIO;
  337. } else if (rfclen > clc_len + 512) {
  338. /*
  339. * Some servers (Windows XP in particular) send more
  340. * data than the lengths in the SMB packet would
  341. * indicate on certain calls (byte range locks and
  342. * trans2 find first calls in particular). While the
  343. * client can handle such a frame by ignoring the
  344. * trailing data, we choose limit the amount of extra
  345. * data to 512 bytes.
  346. */
  347. cifs_dbg(VFS, "RFC1001 size %u more than 512 bytes larger than SMB for mid=%u\n",
  348. rfclen, mid);
  349. return -EIO;
  350. }
  351. }
  352. return 0;
  353. }
  354. bool
  355. is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
  356. {
  357. struct smb_hdr *buf = (struct smb_hdr *)buffer;
  358. struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
  359. struct list_head *tmp, *tmp1, *tmp2;
  360. struct cifs_ses *ses;
  361. struct cifs_tcon *tcon;
  362. struct cifsInodeInfo *pCifsInode;
  363. struct cifsFileInfo *netfile;
  364. cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
  365. if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
  366. (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
  367. struct smb_com_transaction_change_notify_rsp *pSMBr =
  368. (struct smb_com_transaction_change_notify_rsp *)buf;
  369. struct file_notify_information *pnotify;
  370. __u32 data_offset = 0;
  371. if (get_bcc(buf) > sizeof(struct file_notify_information)) {
  372. data_offset = le32_to_cpu(pSMBr->DataOffset);
  373. pnotify = (struct file_notify_information *)
  374. ((char *)&pSMBr->hdr.Protocol + data_offset);
  375. cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
  376. pnotify->FileName, pnotify->Action);
  377. /* cifs_dump_mem("Rcvd notify Data: ",buf,
  378. sizeof(struct smb_hdr)+60); */
  379. return true;
  380. }
  381. if (pSMBr->hdr.Status.CifsError) {
  382. cifs_dbg(FYI, "notify err 0x%x\n",
  383. pSMBr->hdr.Status.CifsError);
  384. return true;
  385. }
  386. return false;
  387. }
  388. if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
  389. return false;
  390. if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
  391. /* no sense logging error on invalid handle on oplock
  392. break - harmless race between close request and oplock
  393. break response is expected from time to time writing out
  394. large dirty files cached on the client */
  395. if ((NT_STATUS_INVALID_HANDLE) ==
  396. le32_to_cpu(pSMB->hdr.Status.CifsError)) {
  397. cifs_dbg(FYI, "invalid handle on oplock break\n");
  398. return true;
  399. } else if (ERRbadfid ==
  400. le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
  401. return true;
  402. } else {
  403. return false; /* on valid oplock brk we get "request" */
  404. }
  405. }
  406. if (pSMB->hdr.WordCount != 8)
  407. return false;
  408. cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
  409. pSMB->LockType, pSMB->OplockLevel);
  410. if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
  411. return false;
  412. /* look up tcon based on tid & uid */
  413. spin_lock(&cifs_tcp_ses_lock);
  414. list_for_each(tmp, &srv->smb_ses_list) {
  415. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  416. list_for_each(tmp1, &ses->tcon_list) {
  417. tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
  418. if (tcon->tid != buf->Tid)
  419. continue;
  420. cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
  421. spin_lock(&tcon->open_file_lock);
  422. list_for_each(tmp2, &tcon->openFileList) {
  423. netfile = list_entry(tmp2, struct cifsFileInfo,
  424. tlist);
  425. if (pSMB->Fid != netfile->fid.netfid)
  426. continue;
  427. cifs_dbg(FYI, "file id match, oplock break\n");
  428. pCifsInode = CIFS_I(d_inode(netfile->dentry));
  429. set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
  430. &pCifsInode->flags);
  431. /*
  432. * Set flag if the server downgrades the oplock
  433. * to L2 else clear.
  434. */
  435. if (pSMB->OplockLevel)
  436. set_bit(
  437. CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2,
  438. &pCifsInode->flags);
  439. else
  440. clear_bit(
  441. CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2,
  442. &pCifsInode->flags);
  443. queue_work(cifsoplockd_wq,
  444. &netfile->oplock_break);
  445. netfile->oplock_break_cancelled = false;
  446. spin_unlock(&tcon->open_file_lock);
  447. spin_unlock(&cifs_tcp_ses_lock);
  448. return true;
  449. }
  450. spin_unlock(&tcon->open_file_lock);
  451. spin_unlock(&cifs_tcp_ses_lock);
  452. cifs_dbg(FYI, "No matching file for oplock break\n");
  453. return true;
  454. }
  455. }
  456. spin_unlock(&cifs_tcp_ses_lock);
  457. cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
  458. return true;
  459. }
  460. void
  461. dump_smb(void *buf, int smb_buf_length)
  462. {
  463. if (traceSMB == 0)
  464. return;
  465. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE, 8, 2, buf,
  466. smb_buf_length, true);
  467. }
  468. void
  469. cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb)
  470. {
  471. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  472. cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
  473. cifs_dbg(VFS, "Autodisabling the use of server inode numbers on %s. This server doesn't seem to support them properly. Hardlinks will not be recognized on this mount. Consider mounting with the \"noserverino\" option to silence this message.\n",
  474. cifs_sb_master_tcon(cifs_sb)->treeName);
  475. }
  476. }
  477. void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
  478. {
  479. oplock &= 0xF;
  480. if (oplock == OPLOCK_EXCLUSIVE) {
  481. cinode->oplock = CIFS_CACHE_WRITE_FLG | CIFS_CACHE_READ_FLG;
  482. cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
  483. &cinode->vfs_inode);
  484. } else if (oplock == OPLOCK_READ) {
  485. cinode->oplock = CIFS_CACHE_READ_FLG;
  486. cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
  487. &cinode->vfs_inode);
  488. } else
  489. cinode->oplock = 0;
  490. }
  491. /*
  492. * We wait for oplock breaks to be processed before we attempt to perform
  493. * writes.
  494. */
  495. int cifs_get_writer(struct cifsInodeInfo *cinode)
  496. {
  497. int rc;
  498. start:
  499. rc = wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK,
  500. TASK_KILLABLE);
  501. if (rc)
  502. return rc;
  503. spin_lock(&cinode->writers_lock);
  504. if (!cinode->writers)
  505. set_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  506. cinode->writers++;
  507. /* Check to see if we have started servicing an oplock break */
  508. if (test_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags)) {
  509. cinode->writers--;
  510. if (cinode->writers == 0) {
  511. clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  512. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
  513. }
  514. spin_unlock(&cinode->writers_lock);
  515. goto start;
  516. }
  517. spin_unlock(&cinode->writers_lock);
  518. return 0;
  519. }
  520. void cifs_put_writer(struct cifsInodeInfo *cinode)
  521. {
  522. spin_lock(&cinode->writers_lock);
  523. cinode->writers--;
  524. if (cinode->writers == 0) {
  525. clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  526. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
  527. }
  528. spin_unlock(&cinode->writers_lock);
  529. }
  530. void cifs_done_oplock_break(struct cifsInodeInfo *cinode)
  531. {
  532. clear_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags);
  533. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK);
  534. }
  535. bool
  536. backup_cred(struct cifs_sb_info *cifs_sb)
  537. {
  538. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID) {
  539. if (uid_eq(cifs_sb->mnt_backupuid, current_fsuid()))
  540. return true;
  541. }
  542. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID) {
  543. if (in_group_p(cifs_sb->mnt_backupgid))
  544. return true;
  545. }
  546. return false;
  547. }
  548. void
  549. cifs_del_pending_open(struct cifs_pending_open *open)
  550. {
  551. spin_lock(&tlink_tcon(open->tlink)->open_file_lock);
  552. list_del(&open->olist);
  553. spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
  554. }
  555. void
  556. cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink,
  557. struct cifs_pending_open *open)
  558. {
  559. #ifdef CONFIG_CIFS_SMB2
  560. memcpy(open->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
  561. #endif
  562. open->oplock = CIFS_OPLOCK_NO_CHANGE;
  563. open->tlink = tlink;
  564. fid->pending_open = open;
  565. list_add_tail(&open->olist, &tlink_tcon(tlink)->pending_opens);
  566. }
  567. void
  568. cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink,
  569. struct cifs_pending_open *open)
  570. {
  571. spin_lock(&tlink_tcon(tlink)->open_file_lock);
  572. cifs_add_pending_open_locked(fid, tlink, open);
  573. spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
  574. }