~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/cifs/misc.c

Version: ~ [ linux-5.18-rc6 ] ~ [ linux-5.17.6 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.38 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.114 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.192 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.241 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.277 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.312 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 22 #include <linux/slab.h>
 23 #include <linux/ctype.h>
 24 #include <linux/mempool.h>
 25 #include <linux/vmalloc.h>
 26 #include "cifspdu.h"
 27 #include "cifsglob.h"
 28 #include "cifsproto.h"
 29 #include "cifs_debug.h"
 30 #include "smberr.h"
 31 #include "nterr.h"
 32 #include "cifs_unicode.h"
 33 #include "smb2pdu.h"
 34 
 35 extern mempool_t *cifs_sm_req_poolp;
 36 extern mempool_t *cifs_req_poolp;
 37 
 38 /* The xid serves as a useful identifier for each incoming vfs request,
 39    in a similar way to the mid which is useful to track each sent smb,
 40    and CurrentXid can also provide a running counter (although it
 41    will eventually wrap past zero) of the total vfs operations handled
 42    since the cifs fs was mounted */
 43 
 44 unsigned int
 45 _get_xid(void)
 46 {
 47         unsigned int xid;
 48 
 49         spin_lock(&GlobalMid_Lock);
 50         GlobalTotalActiveXid++;
 51 
 52         /* keep high water mark for number of simultaneous ops in filesystem */
 53         if (GlobalTotalActiveXid > GlobalMaxActiveXid)
 54                 GlobalMaxActiveXid = GlobalTotalActiveXid;
 55         if (GlobalTotalActiveXid > 65000)
 56                 cifs_dbg(FYI, "warning: more than 65000 requests active\n");
 57         xid = GlobalCurrentXid++;
 58         spin_unlock(&GlobalMid_Lock);
 59         return xid;
 60 }
 61 
 62 void
 63 _free_xid(unsigned int xid)
 64 {
 65         spin_lock(&GlobalMid_Lock);
 66         /* if (GlobalTotalActiveXid == 0)
 67                 BUG(); */
 68         GlobalTotalActiveXid--;
 69         spin_unlock(&GlobalMid_Lock);
 70 }
 71 
 72 struct cifs_ses *
 73 sesInfoAlloc(void)
 74 {
 75         struct cifs_ses *ret_buf;
 76 
 77         ret_buf = kzalloc(sizeof(struct cifs_ses), GFP_KERNEL);
 78         if (ret_buf) {
 79                 atomic_inc(&sesInfoAllocCount);
 80                 ret_buf->status = CifsNew;
 81                 ++ret_buf->ses_count;
 82                 INIT_LIST_HEAD(&ret_buf->smb_ses_list);
 83                 INIT_LIST_HEAD(&ret_buf->tcon_list);
 84                 mutex_init(&ret_buf->session_mutex);
 85                 spin_lock_init(&ret_buf->iface_lock);
 86         }
 87         return ret_buf;
 88 }
 89 
 90 void
 91 sesInfoFree(struct cifs_ses *buf_to_free)
 92 {
 93         if (buf_to_free == NULL) {
 94                 cifs_dbg(FYI, "Null buffer passed to sesInfoFree\n");
 95                 return;
 96         }
 97 
 98         atomic_dec(&sesInfoAllocCount);
 99         kfree(buf_to_free->serverOS);
100         kfree(buf_to_free->serverDomain);
101         kfree(buf_to_free->serverNOS);
102         kzfree(buf_to_free->password);
103         kfree(buf_to_free->user_name);
104         kfree(buf_to_free->domainName);
105         kzfree(buf_to_free->auth_key.response);
106         kfree(buf_to_free->iface_list);
107         kzfree(buf_to_free);
108 }
109 
110 struct cifs_tcon *
111 tconInfoAlloc(void)
112 {
113         struct cifs_tcon *ret_buf;
114 
115         ret_buf = kzalloc(sizeof(*ret_buf), GFP_KERNEL);
116         if (!ret_buf)
117                 return NULL;
118         ret_buf->crfid.fid = kzalloc(sizeof(*ret_buf->crfid.fid), GFP_KERNEL);
119         if (!ret_buf->crfid.fid) {
120                 kfree(ret_buf);
121                 return NULL;
122         }
123 
124         atomic_inc(&tconInfoAllocCount);
125         ret_buf->tidStatus = CifsNew;
126         ++ret_buf->tc_count;
127         INIT_LIST_HEAD(&ret_buf->openFileList);
128         INIT_LIST_HEAD(&ret_buf->tcon_list);
129         spin_lock_init(&ret_buf->open_file_lock);
130         mutex_init(&ret_buf->crfid.fid_mutex);
131         spin_lock_init(&ret_buf->stat_lock);
132         atomic_set(&ret_buf->num_local_opens, 0);
133         atomic_set(&ret_buf->num_remote_opens, 0);
134 
135         return ret_buf;
136 }
137 
138 void
139 tconInfoFree(struct cifs_tcon *buf_to_free)
140 {
141         if (buf_to_free == NULL) {
142                 cifs_dbg(FYI, "Null buffer passed to tconInfoFree\n");
143                 return;
144         }
145         atomic_dec(&tconInfoAllocCount);
146         kfree(buf_to_free->nativeFileSystem);
147         kzfree(buf_to_free->password);
148         kfree(buf_to_free->crfid.fid);
149         kfree(buf_to_free);
150 }
151 
152 struct smb_hdr *
153 cifs_buf_get(void)
154 {
155         struct smb_hdr *ret_buf = NULL;
156         /*
157          * SMB2 header is bigger than CIFS one - no problems to clean some
158          * more bytes for CIFS.
159          */
160         size_t buf_size = sizeof(struct smb2_sync_hdr);
161 
162         /*
163          * We could use negotiated size instead of max_msgsize -
164          * but it may be more efficient to always alloc same size
165          * albeit slightly larger than necessary and maxbuffersize
166          * defaults to this and can not be bigger.
167          */
168         ret_buf = mempool_alloc(cifs_req_poolp, GFP_NOFS);
169 
170         /* clear the first few header bytes */
171         /* for most paths, more is cleared in header_assemble */
172         memset(ret_buf, 0, buf_size + 3);
173         atomic_inc(&bufAllocCount);
174 #ifdef CONFIG_CIFS_STATS2
175         atomic_inc(&totBufAllocCount);
176 #endif /* CONFIG_CIFS_STATS2 */
177 
178         return ret_buf;
179 }
180 
181 void
182 cifs_buf_release(void *buf_to_free)
183 {
184         if (buf_to_free == NULL) {
185                 /* cifs_dbg(FYI, "Null buffer passed to cifs_buf_release\n");*/
186                 return;
187         }
188         mempool_free(buf_to_free, cifs_req_poolp);
189 
190         atomic_dec(&bufAllocCount);
191         return;
192 }
193 
194 struct smb_hdr *
195 cifs_small_buf_get(void)
196 {
197         struct smb_hdr *ret_buf = NULL;
198 
199 /* We could use negotiated size instead of max_msgsize -
200    but it may be more efficient to always alloc same size
201    albeit slightly larger than necessary and maxbuffersize
202    defaults to this and can not be bigger */
203         ret_buf = mempool_alloc(cifs_sm_req_poolp, GFP_NOFS);
204         /* No need to clear memory here, cleared in header assemble */
205         /*      memset(ret_buf, 0, sizeof(struct smb_hdr) + 27);*/
206         atomic_inc(&smBufAllocCount);
207 #ifdef CONFIG_CIFS_STATS2
208         atomic_inc(&totSmBufAllocCount);
209 #endif /* CONFIG_CIFS_STATS2 */
210 
211         return ret_buf;
212 }
213 
214 void
215 cifs_small_buf_release(void *buf_to_free)
216 {
217 
218         if (buf_to_free == NULL) {
219                 cifs_dbg(FYI, "Null buffer passed to cifs_small_buf_release\n");
220                 return;
221         }
222         mempool_free(buf_to_free, cifs_sm_req_poolp);
223 
224         atomic_dec(&smBufAllocCount);
225         return;
226 }
227 
228 void
229 free_rsp_buf(int resp_buftype, void *rsp)
230 {
231         if (resp_buftype == CIFS_SMALL_BUFFER)
232                 cifs_small_buf_release(rsp);
233         else if (resp_buftype == CIFS_LARGE_BUFFER)
234                 cifs_buf_release(rsp);
235 }
236 
237 /* NB: MID can not be set if treeCon not passed in, in that
238    case it is responsbility of caller to set the mid */
239 void
240 header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
241                 const struct cifs_tcon *treeCon, int word_count
242                 /* length of fixed section (word count) in two byte units  */)
243 {
244         char *temp = (char *) buffer;
245 
246         memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
247 
248         buffer->smb_buf_length = cpu_to_be32(
249             (2 * word_count) + sizeof(struct smb_hdr) -
250             4 /*  RFC 1001 length field does not count */  +
251             2 /* for bcc field itself */) ;
252 
253         buffer->Protocol[0] = 0xFF;
254         buffer->Protocol[1] = 'S';
255         buffer->Protocol[2] = 'M';
256         buffer->Protocol[3] = 'B';
257         buffer->Command = smb_command;
258         buffer->Flags = 0x00;   /* case sensitive */
259         buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
260         buffer->Pid = cpu_to_le16((__u16)current->tgid);
261         buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
262         if (treeCon) {
263                 buffer->Tid = treeCon->tid;
264                 if (treeCon->ses) {
265                         if (treeCon->ses->capabilities & CAP_UNICODE)
266                                 buffer->Flags2 |= SMBFLG2_UNICODE;
267                         if (treeCon->ses->capabilities & CAP_STATUS32)
268                                 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
269 
270                         /* Uid is not converted */
271                         buffer->Uid = treeCon->ses->Suid;
272                         buffer->Mid = get_next_mid(treeCon->ses->server);
273                 }
274                 if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
275                         buffer->Flags2 |= SMBFLG2_DFS;
276                 if (treeCon->nocase)
277                         buffer->Flags  |= SMBFLG_CASELESS;
278                 if ((treeCon->ses) && (treeCon->ses->server))
279                         if (treeCon->ses->server->sign)
280                                 buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
281         }
282 
283 /*  endian conversion of flags is now done just before sending */
284         buffer->WordCount = (char) word_count;
285         return;
286 }
287 
288 static int
289 check_smb_hdr(struct smb_hdr *smb)
290 {
291         /* does it have the right SMB "signature" ? */
292         if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff)) {
293                 cifs_dbg(VFS, "Bad protocol string signature header 0x%x\n",
294                          *(unsigned int *)smb->Protocol);
295                 return 1;
296         }
297 
298         /* if it's a response then accept */
299         if (smb->Flags & SMBFLG_RESPONSE)
300                 return 0;
301 
302         /* only one valid case where server sends us request */
303         if (smb->Command == SMB_COM_LOCKING_ANDX)
304                 return 0;
305 
306         cifs_dbg(VFS, "Server sent request, not response. mid=%u\n",
307                  get_mid(smb));
308         return 1;
309 }
310 
311 int
312 checkSMB(char *buf, unsigned int total_read, struct TCP_Server_Info *server)
313 {
314         struct smb_hdr *smb = (struct smb_hdr *)buf;
315         __u32 rfclen = be32_to_cpu(smb->smb_buf_length);
316         __u32 clc_len;  /* calculated length */
317         cifs_dbg(FYI, "checkSMB Length: 0x%x, smb_buf_length: 0x%x\n",
318                  total_read, rfclen);
319 
320         /* is this frame too small to even get to a BCC? */
321         if (total_read < 2 + sizeof(struct smb_hdr)) {
322                 if ((total_read >= sizeof(struct smb_hdr) - 1)
323                             && (smb->Status.CifsError != 0)) {
324                         /* it's an error return */
325                         smb->WordCount = 0;
326                         /* some error cases do not return wct and bcc */
327                         return 0;
328                 } else if ((total_read == sizeof(struct smb_hdr) + 1) &&
329                                 (smb->WordCount == 0)) {
330                         char *tmp = (char *)smb;
331                         /* Need to work around a bug in two servers here */
332                         /* First, check if the part of bcc they sent was zero */
333                         if (tmp[sizeof(struct smb_hdr)] == 0) {
334                                 /* some servers return only half of bcc
335                                  * on simple responses (wct, bcc both zero)
336                                  * in particular have seen this on
337                                  * ulogoffX and FindClose. This leaves
338                                  * one byte of bcc potentially unitialized
339                                  */
340                                 /* zero rest of bcc */
341                                 tmp[sizeof(struct smb_hdr)+1] = 0;
342                                 return 0;
343                         }
344                         cifs_dbg(VFS, "rcvd invalid byte count (bcc)\n");
345                 } else {
346                         cifs_dbg(VFS, "Length less than smb header size\n");
347                 }
348                 return -EIO;
349         }
350 
351         /* otherwise, there is enough to get to the BCC */
352         if (check_smb_hdr(smb))
353                 return -EIO;
354         clc_len = smbCalcSize(smb, server);
355 
356         if (4 + rfclen != total_read) {
357                 cifs_dbg(VFS, "Length read does not match RFC1001 length %d\n",
358                          rfclen);
359                 return -EIO;
360         }
361 
362         if (4 + rfclen != clc_len) {
363                 __u16 mid = get_mid(smb);
364                 /* check if bcc wrapped around for large read responses */
365                 if ((rfclen > 64 * 1024) && (rfclen > clc_len)) {
366                         /* check if lengths match mod 64K */
367                         if (((4 + rfclen) & 0xFFFF) == (clc_len & 0xFFFF))
368                                 return 0; /* bcc wrapped */
369                 }
370                 cifs_dbg(FYI, "Calculated size %u vs length %u mismatch for mid=%u\n",
371                          clc_len, 4 + rfclen, mid);
372 
373                 if (4 + rfclen < clc_len) {
374                         cifs_dbg(VFS, "RFC1001 size %u smaller than SMB for mid=%u\n",
375                                  rfclen, mid);
376                         return -EIO;
377                 } else if (rfclen > clc_len + 512) {
378                         /*
379                          * Some servers (Windows XP in particular) send more
380                          * data than the lengths in the SMB packet would
381                          * indicate on certain calls (byte range locks and
382                          * trans2 find first calls in particular). While the
383                          * client can handle such a frame by ignoring the
384                          * trailing data, we choose limit the amount of extra
385                          * data to 512 bytes.
386                          */
387                         cifs_dbg(VFS, "RFC1001 size %u more than 512 bytes larger than SMB for mid=%u\n",
388                                  rfclen, mid);
389                         return -EIO;
390                 }
391         }
392         return 0;
393 }
394 
395 bool
396 is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
397 {
398         struct smb_hdr *buf = (struct smb_hdr *)buffer;
399         struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
400         struct list_head *tmp, *tmp1, *tmp2;
401         struct cifs_ses *ses;
402         struct cifs_tcon *tcon;
403         struct cifsInodeInfo *pCifsInode;
404         struct cifsFileInfo *netfile;
405 
406         cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
407         if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
408            (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
409                 struct smb_com_transaction_change_notify_rsp *pSMBr =
410                         (struct smb_com_transaction_change_notify_rsp *)buf;
411                 struct file_notify_information *pnotify;
412                 __u32 data_offset = 0;
413                 size_t len = srv->total_read - sizeof(pSMBr->hdr.smb_buf_length);
414 
415                 if (get_bcc(buf) > sizeof(struct file_notify_information)) {
416                         data_offset = le32_to_cpu(pSMBr->DataOffset);
417 
418                         if (data_offset >
419                             len - sizeof(struct file_notify_information)) {
420                                 cifs_dbg(FYI, "invalid data_offset %u\n",
421                                          data_offset);
422                                 return true;
423                         }
424                         pnotify = (struct file_notify_information *)
425                                 ((char *)&pSMBr->hdr.Protocol + data_offset);
426                         cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
427                                  pnotify->FileName, pnotify->Action);
428                         /*   cifs_dump_mem("Rcvd notify Data: ",buf,
429                                 sizeof(struct smb_hdr)+60); */
430                         return true;
431                 }
432                 if (pSMBr->hdr.Status.CifsError) {
433                         cifs_dbg(FYI, "notify err 0x%x\n",
434                                  pSMBr->hdr.Status.CifsError);
435                         return true;
436                 }
437                 return false;
438         }
439         if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
440                 return false;
441         if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
442                 /* no sense logging error on invalid handle on oplock
443                    break - harmless race between close request and oplock
444                    break response is expected from time to time writing out
445                    large dirty files cached on the client */
446                 if ((NT_STATUS_INVALID_HANDLE) ==
447                    le32_to_cpu(pSMB->hdr.Status.CifsError)) {
448                         cifs_dbg(FYI, "invalid handle on oplock break\n");
449                         return true;
450                 } else if (ERRbadfid ==
451                    le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
452                         return true;
453                 } else {
454                         return false; /* on valid oplock brk we get "request" */
455                 }
456         }
457         if (pSMB->hdr.WordCount != 8)
458                 return false;
459 
460         cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
461                  pSMB->LockType, pSMB->OplockLevel);
462         if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
463                 return false;
464 
465         /* look up tcon based on tid & uid */
466         spin_lock(&cifs_tcp_ses_lock);
467         list_for_each(tmp, &srv->smb_ses_list) {
468                 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
469                 list_for_each(tmp1, &ses->tcon_list) {
470                         tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
471                         if (tcon->tid != buf->Tid)
472                                 continue;
473 
474                         cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
475                         spin_lock(&tcon->open_file_lock);
476                         list_for_each(tmp2, &tcon->openFileList) {
477                                 netfile = list_entry(tmp2, struct cifsFileInfo,
478                                                      tlist);
479                                 if (pSMB->Fid != netfile->fid.netfid)
480                                         continue;
481 
482                                 cifs_dbg(FYI, "file id match, oplock break\n");
483                                 pCifsInode = CIFS_I(d_inode(netfile->dentry));
484 
485                                 set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
486                                         &pCifsInode->flags);
487 
488                                 /*
489                                  * Set flag if the server downgrades the oplock
490                                  * to L2 else clear.
491                                  */
492                                 if (pSMB->OplockLevel)
493                                         set_bit(
494                                            CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2,
495                                            &pCifsInode->flags);
496                                 else
497                                         clear_bit(
498                                            CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2,
499                                            &pCifsInode->flags);
500 
501                                 queue_work(cifsoplockd_wq,
502                                            &netfile->oplock_break);
503                                 netfile->oplock_break_cancelled = false;
504 
505                                 spin_unlock(&tcon->open_file_lock);
506                                 spin_unlock(&cifs_tcp_ses_lock);
507                                 return true;
508                         }
509                         spin_unlock(&tcon->open_file_lock);
510                         spin_unlock(&cifs_tcp_ses_lock);
511                         cifs_dbg(FYI, "No matching file for oplock break\n");
512                         return true;
513                 }
514         }
515         spin_unlock(&cifs_tcp_ses_lock);
516         cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
517         return true;
518 }
519 
520 void
521 dump_smb(void *buf, int smb_buf_length)
522 {
523         if (traceSMB == 0)
524                 return;
525 
526         print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE, 8, 2, buf,
527                        smb_buf_length, true);
528 }
529 
530 void
531 cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb)
532 {
533         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
534                 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
535                 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",
536                          cifs_sb_master_tcon(cifs_sb)->treeName);
537         }
538 }
539 
540 void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
541 {
542         oplock &= 0xF;
543 
544         if (oplock == OPLOCK_EXCLUSIVE) {
545                 cinode->oplock = CIFS_CACHE_WRITE_FLG | CIFS_CACHE_READ_FLG;
546                 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
547                          &cinode->vfs_inode);
548         } else if (oplock == OPLOCK_READ) {
549                 cinode->oplock = CIFS_CACHE_READ_FLG;
550                 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
551                          &cinode->vfs_inode);
552         } else
553                 cinode->oplock = 0;
554 }
555 
556 /*
557  * We wait for oplock breaks to be processed before we attempt to perform
558  * writes.
559  */
560 int cifs_get_writer(struct cifsInodeInfo *cinode)
561 {
562         int rc;
563 
564 start:
565         rc = wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK,
566                          TASK_KILLABLE);
567         if (rc)
568                 return rc;
569 
570         spin_lock(&cinode->writers_lock);
571         if (!cinode->writers)
572                 set_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
573         cinode->writers++;
574         /* Check to see if we have started servicing an oplock break */
575         if (test_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags)) {
576                 cinode->writers--;
577                 if (cinode->writers == 0) {
578                         clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
579                         wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
580                 }
581                 spin_unlock(&cinode->writers_lock);
582                 goto start;
583         }
584         spin_unlock(&cinode->writers_lock);
585         return 0;
586 }
587 
588 void cifs_put_writer(struct cifsInodeInfo *cinode)
589 {
590         spin_lock(&cinode->writers_lock);
591         cinode->writers--;
592         if (cinode->writers == 0) {
593                 clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
594                 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
595         }
596         spin_unlock(&cinode->writers_lock);
597 }
598 
599 void cifs_done_oplock_break(struct cifsInodeInfo *cinode)
600 {
601         clear_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags);
602         wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK);
603 }
604 
605 bool
606 backup_cred(struct cifs_sb_info *cifs_sb)
607 {
608         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID) {
609                 if (uid_eq(cifs_sb->mnt_backupuid, current_fsuid()))
610                         return true;
611         }
612         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID) {
613                 if (in_group_p(cifs_sb->mnt_backupgid))
614                         return true;
615         }
616 
617         return false;
618 }
619 
620 void
621 cifs_del_pending_open(struct cifs_pending_open *open)
622 {
623         spin_lock(&tlink_tcon(open->tlink)->open_file_lock);
624         list_del(&open->olist);
625         spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
626 }
627 
628 void
629 cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink,
630                              struct cifs_pending_open *open)
631 {
632         memcpy(open->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
633         open->oplock = CIFS_OPLOCK_NO_CHANGE;
634         open->tlink = tlink;
635         fid->pending_open = open;
636         list_add_tail(&open->olist, &tlink_tcon(tlink)->pending_opens);
637 }
638 
639 void
640 cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink,
641                       struct cifs_pending_open *open)
642 {
643         spin_lock(&tlink_tcon(tlink)->open_file_lock);
644         cifs_add_pending_open_locked(fid, tlink, open);
645         spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
646 }
647 
648 /* parses DFS refferal V3 structure
649  * caller is responsible for freeing target_nodes
650  * returns:
651  * - on success - 0
652  * - on failure - errno
653  */
654 int
655 parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size,
656                     unsigned int *num_of_nodes,
657                     struct dfs_info3_param **target_nodes,
658                     const struct nls_table *nls_codepage, int remap,
659                     const char *searchName, bool is_unicode)
660 {
661         int i, rc = 0;
662         char *data_end;
663         struct dfs_referral_level_3 *ref;
664 
665         *num_of_nodes = le16_to_cpu(rsp->NumberOfReferrals);
666 
667         if (*num_of_nodes < 1) {
668                 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
669                          *num_of_nodes);
670                 rc = -EINVAL;
671                 goto parse_DFS_referrals_exit;
672         }
673 
674         ref = (struct dfs_referral_level_3 *) &(rsp->referrals);
675         if (ref->VersionNumber != cpu_to_le16(3)) {
676                 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
677                          le16_to_cpu(ref->VersionNumber));
678                 rc = -EINVAL;
679                 goto parse_DFS_referrals_exit;
680         }
681 
682         /* get the upper boundary of the resp buffer */
683         data_end = (char *)rsp + rsp_size;
684 
685         cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
686                  *num_of_nodes, le32_to_cpu(rsp->DFSFlags));
687 
688         *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
689                                 GFP_KERNEL);
690         if (*target_nodes == NULL) {
691                 rc = -ENOMEM;
692                 goto parse_DFS_referrals_exit;
693         }
694 
695         /* collect necessary data from referrals */
696         for (i = 0; i < *num_of_nodes; i++) {
697                 char *temp;
698                 int max_len;
699                 struct dfs_info3_param *node = (*target_nodes)+i;
700 
701                 node->flags = le32_to_cpu(rsp->DFSFlags);
702                 if (is_unicode) {
703                         __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
704                                                 GFP_KERNEL);
705                         if (tmp == NULL) {
706                                 rc = -ENOMEM;
707                                 goto parse_DFS_referrals_exit;
708                         }
709                         cifsConvertToUTF16((__le16 *) tmp, searchName,
710                                            PATH_MAX, nls_codepage, remap);
711                         node->path_consumed = cifs_utf16_bytes(tmp,
712                                         le16_to_cpu(rsp->PathConsumed),
713                                         nls_codepage);
714                         kfree(tmp);
715                 } else
716                         node->path_consumed = le16_to_cpu(rsp->PathConsumed);
717 
718                 node->server_type = le16_to_cpu(ref->ServerType);
719                 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
720 
721                 /* copy DfsPath */
722                 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
723                 max_len = data_end - temp;
724                 node->path_name = cifs_strndup_from_utf16(temp, max_len,
725                                                 is_unicode, nls_codepage);
726                 if (!node->path_name) {
727                         rc = -ENOMEM;
728                         goto parse_DFS_referrals_exit;
729                 }
730 
731                 /* copy link target UNC */
732                 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
733                 max_len = data_end - temp;
734                 node->node_name = cifs_strndup_from_utf16(temp, max_len,
735                                                 is_unicode, nls_codepage);
736                 if (!node->node_name) {
737                         rc = -ENOMEM;
738                         goto parse_DFS_referrals_exit;
739                 }
740 
741                 ref++;
742         }
743 
744 parse_DFS_referrals_exit:
745         if (rc) {
746                 free_dfs_info_array(*target_nodes, *num_of_nodes);
747                 *target_nodes = NULL;
748                 *num_of_nodes = 0;
749         }
750         return rc;
751 }
752 
753 struct cifs_aio_ctx *
754 cifs_aio_ctx_alloc(void)
755 {
756         struct cifs_aio_ctx *ctx;
757 
758         ctx = kzalloc(sizeof(struct cifs_aio_ctx), GFP_KERNEL);
759         if (!ctx)
760                 return NULL;
761 
762         INIT_LIST_HEAD(&ctx->list);
763         mutex_init(&ctx->aio_mutex);
764         init_completion(&ctx->done);
765         kref_init(&ctx->refcount);
766         return ctx;
767 }
768 
769 void
770 cifs_aio_ctx_release(struct kref *refcount)
771 {
772         struct cifs_aio_ctx *ctx = container_of(refcount,
773                                         struct cifs_aio_ctx, refcount);
774 
775         cifsFileInfo_put(ctx->cfile);
776         kvfree(ctx->bv);
777         kfree(ctx);
778 }
779 
780 #define CIFS_AIO_KMALLOC_LIMIT (1024 * 1024)
781 
782 int
783 setup_aio_ctx_iter(struct cifs_aio_ctx *ctx, struct iov_iter *iter, int rw)
784 {
785         ssize_t rc;
786         unsigned int cur_npages;
787         unsigned int npages = 0;
788         unsigned int i;
789         size_t len;
790         size_t count = iov_iter_count(iter);
791         unsigned int saved_len;
792         size_t start;
793         unsigned int max_pages = iov_iter_npages(iter, INT_MAX);
794         struct page **pages = NULL;
795         struct bio_vec *bv = NULL;
796 
797         if (iov_iter_is_kvec(iter)) {
798                 memcpy(&ctx->iter, iter, sizeof(struct iov_iter));
799                 ctx->len = count;
800                 iov_iter_advance(iter, count);
801                 return 0;
802         }
803 
804         if (max_pages * sizeof(struct bio_vec) <= CIFS_AIO_KMALLOC_LIMIT)
805                 bv = kmalloc_array(max_pages, sizeof(struct bio_vec),
806                                    GFP_KERNEL);
807 
808         if (!bv) {
809                 bv = vmalloc(array_size(max_pages, sizeof(struct bio_vec)));
810                 if (!bv)
811                         return -ENOMEM;
812         }
813 
814         if (max_pages * sizeof(struct page *) <= CIFS_AIO_KMALLOC_LIMIT)
815                 pages = kmalloc_array(max_pages, sizeof(struct page *),
816                                       GFP_KERNEL);
817 
818         if (!pages) {
819                 pages = vmalloc(array_size(max_pages, sizeof(struct page *)));
820                 if (!pages) {
821                         kvfree(bv);
822                         return -ENOMEM;
823                 }
824         }
825 
826         saved_len = count;
827 
828         while (count && npages < max_pages) {
829                 rc = iov_iter_get_pages(iter, pages, count, max_pages, &start);
830                 if (rc < 0) {
831                         cifs_dbg(VFS, "couldn't get user pages (rc=%zd)\n", rc);
832                         break;
833                 }
834 
835                 if (rc > count) {
836                         cifs_dbg(VFS, "get pages rc=%zd more than %zu\n", rc,
837                                  count);
838                         break;
839                 }
840 
841                 iov_iter_advance(iter, rc);
842                 count -= rc;
843                 rc += start;
844                 cur_npages = DIV_ROUND_UP(rc, PAGE_SIZE);
845 
846                 if (npages + cur_npages > max_pages) {
847                         cifs_dbg(VFS, "out of vec array capacity (%u vs %u)\n",
848                                  npages + cur_npages, max_pages);
849                         break;
850                 }
851 
852                 for (i = 0; i < cur_npages; i++) {
853                         len = rc > PAGE_SIZE ? PAGE_SIZE : rc;
854                         bv[npages + i].bv_page = pages[i];
855                         bv[npages + i].bv_offset = start;
856                         bv[npages + i].bv_len = len - start;
857                         rc -= len;
858                         start = 0;
859                 }
860 
861                 npages += cur_npages;
862         }
863 
864         kvfree(pages);
865         ctx->bv = bv;
866         ctx->len = saved_len - count;
867         ctx->npages = npages;
868         iov_iter_bvec(&ctx->iter, rw, ctx->bv, npages, ctx->len);
869         return 0;
870 }
871 
872 /**
873  * cifs_alloc_hash - allocate hash and hash context together
874  *
875  * The caller has to make sure @sdesc is initialized to either NULL or
876  * a valid context. Both can be freed via cifs_free_hash().
877  */
878 int
879 cifs_alloc_hash(const char *name,
880                 struct crypto_shash **shash, struct sdesc **sdesc)
881 {
882         int rc = 0;
883         size_t size;
884 
885         if (*sdesc != NULL)
886                 return 0;
887 
888         *shash = crypto_alloc_shash(name, 0, 0);
889         if (IS_ERR(*shash)) {
890                 cifs_dbg(VFS, "could not allocate crypto %s\n", name);
891                 rc = PTR_ERR(*shash);
892                 *shash = NULL;
893                 *sdesc = NULL;
894                 return rc;
895         }
896 
897         size = sizeof(struct shash_desc) + crypto_shash_descsize(*shash);
898         *sdesc = kmalloc(size, GFP_KERNEL);
899         if (*sdesc == NULL) {
900                 cifs_dbg(VFS, "no memory left to allocate crypto %s\n", name);
901                 crypto_free_shash(*shash);
902                 *shash = NULL;
903                 return -ENOMEM;
904         }
905 
906         (*sdesc)->shash.tfm = *shash;
907         (*sdesc)->shash.flags = 0x0;
908         return 0;
909 }
910 
911 /**
912  * cifs_free_hash - free hash and hash context together
913  *
914  * Freeing a NULL hash or context is safe.
915  */
916 void
917 cifs_free_hash(struct crypto_shash **shash, struct sdesc **sdesc)
918 {
919         kfree(*sdesc);
920         *sdesc = NULL;
921         if (*shash)
922                 crypto_free_shash(*shash);
923         *shash = NULL;
924 }
925 
926 /**
927  * rqst_page_get_length - obtain the length and offset for a page in smb_rqst
928  * Input: rqst - a smb_rqst, page - a page index for rqst
929  * Output: *len - the length for this page, *offset - the offset for this page
930  */
931 void rqst_page_get_length(struct smb_rqst *rqst, unsigned int page,
932                                 unsigned int *len, unsigned int *offset)
933 {
934         *len = rqst->rq_pagesz;
935         *offset = (page == 0) ? rqst->rq_offset : 0;
936 
937         if (rqst->rq_npages == 1 || page == rqst->rq_npages-1)
938                 *len = rqst->rq_tailsz;
939         else if (page == 0)
940                 *len = rqst->rq_pagesz - rqst->rq_offset;
941 }
942 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp