misc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. kzfree(buf_to_free->password);
  92. kfree(buf_to_free->user_name);
  93. kfree(buf_to_free->domainName);
  94. kzfree(buf_to_free->auth_key.response);
  95. kzfree(buf_to_free);
  96. }
  97. struct cifs_tcon *
  98. tconInfoAlloc(void)
  99. {
  100. struct cifs_tcon *ret_buf;
  101. ret_buf = kzalloc(sizeof(struct cifs_tcon), GFP_KERNEL);
  102. if (ret_buf) {
  103. atomic_inc(&tconInfoAllocCount);
  104. ret_buf->tidStatus = CifsNew;
  105. ++ret_buf->tc_count;
  106. INIT_LIST_HEAD(&ret_buf->openFileList);
  107. INIT_LIST_HEAD(&ret_buf->tcon_list);
  108. spin_lock_init(&ret_buf->open_file_lock);
  109. #ifdef CONFIG_CIFS_STATS
  110. spin_lock_init(&ret_buf->stat_lock);
  111. #endif
  112. }
  113. return ret_buf;
  114. }
  115. void
  116. tconInfoFree(struct cifs_tcon *buf_to_free)
  117. {
  118. if (buf_to_free == NULL) {
  119. cifs_dbg(FYI, "Null buffer passed to tconInfoFree\n");
  120. return;
  121. }
  122. atomic_dec(&tconInfoAllocCount);
  123. kfree(buf_to_free->nativeFileSystem);
  124. kzfree(buf_to_free->password);
  125. kfree(buf_to_free);
  126. }
  127. struct smb_hdr *
  128. cifs_buf_get(void)
  129. {
  130. struct smb_hdr *ret_buf = NULL;
  131. size_t buf_size = sizeof(struct smb_hdr);
  132. #ifdef CONFIG_CIFS_SMB2
  133. /*
  134. * SMB2 header is bigger than CIFS one - no problems to clean some
  135. * more bytes for CIFS.
  136. */
  137. buf_size = sizeof(struct smb2_hdr);
  138. #endif
  139. /*
  140. * We could use negotiated size instead of max_msgsize -
  141. * but it may be more efficient to always alloc same size
  142. * albeit slightly larger than necessary and maxbuffersize
  143. * defaults to this and can not be bigger.
  144. */
  145. ret_buf = mempool_alloc(cifs_req_poolp, GFP_NOFS);
  146. /* clear the first few header bytes */
  147. /* for most paths, more is cleared in header_assemble */
  148. if (ret_buf) {
  149. memset(ret_buf, 0, buf_size + 3);
  150. atomic_inc(&bufAllocCount);
  151. #ifdef CONFIG_CIFS_STATS2
  152. atomic_inc(&totBufAllocCount);
  153. #endif /* CONFIG_CIFS_STATS2 */
  154. }
  155. return ret_buf;
  156. }
  157. void
  158. cifs_buf_release(void *buf_to_free)
  159. {
  160. if (buf_to_free == NULL) {
  161. /* cifs_dbg(FYI, "Null buffer passed to cifs_buf_release\n");*/
  162. return;
  163. }
  164. mempool_free(buf_to_free, cifs_req_poolp);
  165. atomic_dec(&bufAllocCount);
  166. return;
  167. }
  168. struct smb_hdr *
  169. cifs_small_buf_get(void)
  170. {
  171. struct smb_hdr *ret_buf = NULL;
  172. /* We could use negotiated size instead of max_msgsize -
  173. but it may be more efficient to always alloc same size
  174. albeit slightly larger than necessary and maxbuffersize
  175. defaults to this and can not be bigger */
  176. ret_buf = mempool_alloc(cifs_sm_req_poolp, GFP_NOFS);
  177. if (ret_buf) {
  178. /* No need to clear memory here, cleared in header assemble */
  179. /* memset(ret_buf, 0, sizeof(struct smb_hdr) + 27);*/
  180. atomic_inc(&smBufAllocCount);
  181. #ifdef CONFIG_CIFS_STATS2
  182. atomic_inc(&totSmBufAllocCount);
  183. #endif /* CONFIG_CIFS_STATS2 */
  184. }
  185. return ret_buf;
  186. }
  187. void
  188. cifs_small_buf_release(void *buf_to_free)
  189. {
  190. if (buf_to_free == NULL) {
  191. cifs_dbg(FYI, "Null buffer passed to cifs_small_buf_release\n");
  192. return;
  193. }
  194. mempool_free(buf_to_free, cifs_sm_req_poolp);
  195. atomic_dec(&smBufAllocCount);
  196. return;
  197. }
  198. void
  199. free_rsp_buf(int resp_buftype, void *rsp)
  200. {
  201. if (resp_buftype == CIFS_SMALL_BUFFER)
  202. cifs_small_buf_release(rsp);
  203. else if (resp_buftype == CIFS_LARGE_BUFFER)
  204. cifs_buf_release(rsp);
  205. }
  206. /* NB: MID can not be set if treeCon not passed in, in that
  207. case it is responsbility of caller to set the mid */
  208. void
  209. header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
  210. const struct cifs_tcon *treeCon, int word_count
  211. /* length of fixed section (word count) in two byte units */)
  212. {
  213. char *temp = (char *) buffer;
  214. memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
  215. buffer->smb_buf_length = cpu_to_be32(
  216. (2 * word_count) + sizeof(struct smb_hdr) -
  217. 4 /* RFC 1001 length field does not count */ +
  218. 2 /* for bcc field itself */) ;
  219. buffer->Protocol[0] = 0xFF;
  220. buffer->Protocol[1] = 'S';
  221. buffer->Protocol[2] = 'M';
  222. buffer->Protocol[3] = 'B';
  223. buffer->Command = smb_command;
  224. buffer->Flags = 0x00; /* case sensitive */
  225. buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
  226. buffer->Pid = cpu_to_le16((__u16)current->tgid);
  227. buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
  228. if (treeCon) {
  229. buffer->Tid = treeCon->tid;
  230. if (treeCon->ses) {
  231. if (treeCon->ses->capabilities & CAP_UNICODE)
  232. buffer->Flags2 |= SMBFLG2_UNICODE;
  233. if (treeCon->ses->capabilities & CAP_STATUS32)
  234. buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  235. /* Uid is not converted */
  236. buffer->Uid = treeCon->ses->Suid;
  237. buffer->Mid = get_next_mid(treeCon->ses->server);
  238. }
  239. if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
  240. buffer->Flags2 |= SMBFLG2_DFS;
  241. if (treeCon->nocase)
  242. buffer->Flags |= SMBFLG_CASELESS;
  243. if ((treeCon->ses) && (treeCon->ses->server))
  244. if (treeCon->ses->server->sign)
  245. buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  246. }
  247. /* endian conversion of flags is now done just before sending */
  248. buffer->WordCount = (char) word_count;
  249. return;
  250. }
  251. static int
  252. check_smb_hdr(struct smb_hdr *smb)
  253. {
  254. /* does it have the right SMB "signature" ? */
  255. if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff)) {
  256. cifs_dbg(VFS, "Bad protocol string signature header 0x%x\n",
  257. *(unsigned int *)smb->Protocol);
  258. return 1;
  259. }
  260. /* if it's a response then accept */
  261. if (smb->Flags & SMBFLG_RESPONSE)
  262. return 0;
  263. /* only one valid case where server sends us request */
  264. if (smb->Command == SMB_COM_LOCKING_ANDX)
  265. return 0;
  266. cifs_dbg(VFS, "Server sent request, not response. mid=%u\n",
  267. get_mid(smb));
  268. return 1;
  269. }
  270. int
  271. checkSMB(char *buf, unsigned int total_read, struct TCP_Server_Info *server)
  272. {
  273. struct smb_hdr *smb = (struct smb_hdr *)buf;
  274. __u32 rfclen = be32_to_cpu(smb->smb_buf_length);
  275. __u32 clc_len; /* calculated length */
  276. cifs_dbg(FYI, "checkSMB Length: 0x%x, smb_buf_length: 0x%x\n",
  277. total_read, rfclen);
  278. /* is this frame too small to even get to a BCC? */
  279. if (total_read < 2 + sizeof(struct smb_hdr)) {
  280. if ((total_read >= sizeof(struct smb_hdr) - 1)
  281. && (smb->Status.CifsError != 0)) {
  282. /* it's an error return */
  283. smb->WordCount = 0;
  284. /* some error cases do not return wct and bcc */
  285. return 0;
  286. } else if ((total_read == sizeof(struct smb_hdr) + 1) &&
  287. (smb->WordCount == 0)) {
  288. char *tmp = (char *)smb;
  289. /* Need to work around a bug in two servers here */
  290. /* First, check if the part of bcc they sent was zero */
  291. if (tmp[sizeof(struct smb_hdr)] == 0) {
  292. /* some servers return only half of bcc
  293. * on simple responses (wct, bcc both zero)
  294. * in particular have seen this on
  295. * ulogoffX and FindClose. This leaves
  296. * one byte of bcc potentially unitialized
  297. */
  298. /* zero rest of bcc */
  299. tmp[sizeof(struct smb_hdr)+1] = 0;
  300. return 0;
  301. }
  302. cifs_dbg(VFS, "rcvd invalid byte count (bcc)\n");
  303. } else {
  304. cifs_dbg(VFS, "Length less than smb header size\n");
  305. }
  306. return -EIO;
  307. }
  308. /* otherwise, there is enough to get to the BCC */
  309. if (check_smb_hdr(smb))
  310. return -EIO;
  311. clc_len = smbCalcSize(smb);
  312. if (4 + rfclen != total_read) {
  313. cifs_dbg(VFS, "Length read does not match RFC1001 length %d\n",
  314. rfclen);
  315. return -EIO;
  316. }
  317. if (4 + rfclen != clc_len) {
  318. __u16 mid = get_mid(smb);
  319. /* check if bcc wrapped around for large read responses */
  320. if ((rfclen > 64 * 1024) && (rfclen > clc_len)) {
  321. /* check if lengths match mod 64K */
  322. if (((4 + rfclen) & 0xFFFF) == (clc_len & 0xFFFF))
  323. return 0; /* bcc wrapped */
  324. }
  325. cifs_dbg(FYI, "Calculated size %u vs length %u mismatch for mid=%u\n",
  326. clc_len, 4 + rfclen, mid);
  327. if (4 + rfclen < clc_len) {
  328. cifs_dbg(VFS, "RFC1001 size %u smaller than SMB for mid=%u\n",
  329. rfclen, mid);
  330. return -EIO;
  331. } else if (rfclen > clc_len + 512) {
  332. /*
  333. * Some servers (Windows XP in particular) send more
  334. * data than the lengths in the SMB packet would
  335. * indicate on certain calls (byte range locks and
  336. * trans2 find first calls in particular). While the
  337. * client can handle such a frame by ignoring the
  338. * trailing data, we choose limit the amount of extra
  339. * data to 512 bytes.
  340. */
  341. cifs_dbg(VFS, "RFC1001 size %u more than 512 bytes larger than SMB for mid=%u\n",
  342. rfclen, mid);
  343. return -EIO;
  344. }
  345. }
  346. return 0;
  347. }
  348. bool
  349. is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
  350. {
  351. struct smb_hdr *buf = (struct smb_hdr *)buffer;
  352. struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
  353. struct list_head *tmp, *tmp1, *tmp2;
  354. struct cifs_ses *ses;
  355. struct cifs_tcon *tcon;
  356. struct cifsInodeInfo *pCifsInode;
  357. struct cifsFileInfo *netfile;
  358. cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
  359. if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
  360. (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
  361. struct smb_com_transaction_change_notify_rsp *pSMBr =
  362. (struct smb_com_transaction_change_notify_rsp *)buf;
  363. struct file_notify_information *pnotify;
  364. __u32 data_offset = 0;
  365. if (get_bcc(buf) > sizeof(struct file_notify_information)) {
  366. data_offset = le32_to_cpu(pSMBr->DataOffset);
  367. pnotify = (struct file_notify_information *)
  368. ((char *)&pSMBr->hdr.Protocol + data_offset);
  369. cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
  370. pnotify->FileName, pnotify->Action);
  371. /* cifs_dump_mem("Rcvd notify Data: ",buf,
  372. sizeof(struct smb_hdr)+60); */
  373. return true;
  374. }
  375. if (pSMBr->hdr.Status.CifsError) {
  376. cifs_dbg(FYI, "notify err 0x%x\n",
  377. pSMBr->hdr.Status.CifsError);
  378. return true;
  379. }
  380. return false;
  381. }
  382. if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
  383. return false;
  384. if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
  385. /* no sense logging error on invalid handle on oplock
  386. break - harmless race between close request and oplock
  387. break response is expected from time to time writing out
  388. large dirty files cached on the client */
  389. if ((NT_STATUS_INVALID_HANDLE) ==
  390. le32_to_cpu(pSMB->hdr.Status.CifsError)) {
  391. cifs_dbg(FYI, "invalid handle on oplock break\n");
  392. return true;
  393. } else if (ERRbadfid ==
  394. le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
  395. return true;
  396. } else {
  397. return false; /* on valid oplock brk we get "request" */
  398. }
  399. }
  400. if (pSMB->hdr.WordCount != 8)
  401. return false;
  402. cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
  403. pSMB->LockType, pSMB->OplockLevel);
  404. if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
  405. return false;
  406. /* look up tcon based on tid & uid */
  407. spin_lock(&cifs_tcp_ses_lock);
  408. list_for_each(tmp, &srv->smb_ses_list) {
  409. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  410. list_for_each(tmp1, &ses->tcon_list) {
  411. tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
  412. if (tcon->tid != buf->Tid)
  413. continue;
  414. cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
  415. spin_lock(&tcon->open_file_lock);
  416. list_for_each(tmp2, &tcon->openFileList) {
  417. netfile = list_entry(tmp2, struct cifsFileInfo,
  418. tlist);
  419. if (pSMB->Fid != netfile->fid.netfid)
  420. continue;
  421. cifs_dbg(FYI, "file id match, oplock break\n");
  422. pCifsInode = CIFS_I(d_inode(netfile->dentry));
  423. set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
  424. &pCifsInode->flags);
  425. /*
  426. * Set flag if the server downgrades the oplock
  427. * to L2 else clear.
  428. */
  429. if (pSMB->OplockLevel)
  430. set_bit(
  431. CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2,
  432. &pCifsInode->flags);
  433. else
  434. clear_bit(
  435. CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2,
  436. &pCifsInode->flags);
  437. queue_work(cifsoplockd_wq,
  438. &netfile->oplock_break);
  439. netfile->oplock_break_cancelled = false;
  440. spin_unlock(&tcon->open_file_lock);
  441. spin_unlock(&cifs_tcp_ses_lock);
  442. return true;
  443. }
  444. spin_unlock(&tcon->open_file_lock);
  445. spin_unlock(&cifs_tcp_ses_lock);
  446. cifs_dbg(FYI, "No matching file for oplock break\n");
  447. return true;
  448. }
  449. }
  450. spin_unlock(&cifs_tcp_ses_lock);
  451. cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
  452. return true;
  453. }
  454. void
  455. dump_smb(void *buf, int smb_buf_length)
  456. {
  457. if (traceSMB == 0)
  458. return;
  459. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE, 8, 2, buf,
  460. smb_buf_length, true);
  461. }
  462. void
  463. cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb)
  464. {
  465. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  466. cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
  467. 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",
  468. cifs_sb_master_tcon(cifs_sb)->treeName);
  469. }
  470. }
  471. void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
  472. {
  473. oplock &= 0xF;
  474. if (oplock == OPLOCK_EXCLUSIVE) {
  475. cinode->oplock = CIFS_CACHE_WRITE_FLG | CIFS_CACHE_READ_FLG;
  476. cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
  477. &cinode->vfs_inode);
  478. } else if (oplock == OPLOCK_READ) {
  479. cinode->oplock = CIFS_CACHE_READ_FLG;
  480. cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
  481. &cinode->vfs_inode);
  482. } else
  483. cinode->oplock = 0;
  484. }
  485. /*
  486. * We wait for oplock breaks to be processed before we attempt to perform
  487. * writes.
  488. */
  489. int cifs_get_writer(struct cifsInodeInfo *cinode)
  490. {
  491. int rc;
  492. start:
  493. rc = wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK,
  494. TASK_KILLABLE);
  495. if (rc)
  496. return rc;
  497. spin_lock(&cinode->writers_lock);
  498. if (!cinode->writers)
  499. set_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  500. cinode->writers++;
  501. /* Check to see if we have started servicing an oplock break */
  502. if (test_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags)) {
  503. cinode->writers--;
  504. if (cinode->writers == 0) {
  505. clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  506. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
  507. }
  508. spin_unlock(&cinode->writers_lock);
  509. goto start;
  510. }
  511. spin_unlock(&cinode->writers_lock);
  512. return 0;
  513. }
  514. void cifs_put_writer(struct cifsInodeInfo *cinode)
  515. {
  516. spin_lock(&cinode->writers_lock);
  517. cinode->writers--;
  518. if (cinode->writers == 0) {
  519. clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  520. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
  521. }
  522. spin_unlock(&cinode->writers_lock);
  523. }
  524. void cifs_done_oplock_break(struct cifsInodeInfo *cinode)
  525. {
  526. clear_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags);
  527. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK);
  528. }
  529. bool
  530. backup_cred(struct cifs_sb_info *cifs_sb)
  531. {
  532. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID) {
  533. if (uid_eq(cifs_sb->mnt_backupuid, current_fsuid()))
  534. return true;
  535. }
  536. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID) {
  537. if (in_group_p(cifs_sb->mnt_backupgid))
  538. return true;
  539. }
  540. return false;
  541. }
  542. void
  543. cifs_del_pending_open(struct cifs_pending_open *open)
  544. {
  545. spin_lock(&tlink_tcon(open->tlink)->open_file_lock);
  546. list_del(&open->olist);
  547. spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
  548. }
  549. void
  550. cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink,
  551. struct cifs_pending_open *open)
  552. {
  553. #ifdef CONFIG_CIFS_SMB2
  554. memcpy(open->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
  555. #endif
  556. open->oplock = CIFS_OPLOCK_NO_CHANGE;
  557. open->tlink = tlink;
  558. fid->pending_open = open;
  559. list_add_tail(&open->olist, &tlink_tcon(tlink)->pending_opens);
  560. }
  561. void
  562. cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink,
  563. struct cifs_pending_open *open)
  564. {
  565. spin_lock(&tlink_tcon(tlink)->open_file_lock);
  566. cifs_add_pending_open_locked(fid, tlink, open);
  567. spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
  568. }