blob: 2c2acc03b24eb1e3145d0339a8e9470986b14e92 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * fs/cifs/inode.c
3 *
4 * Copyright (C) International Business Machines Corp., 2002,2010
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/fs.h>
22#include <linux/stat.h>
23#include <linux/slab.h>
24#include <linux/pagemap.h>
25#include <asm/div64.h>
26#include "cifsfs.h"
27#include "cifspdu.h"
28#include "cifsglob.h"
29#include "cifsproto.h"
30#include "cifs_debug.h"
31#include "cifs_fs_sb.h"
32#include "fscache.h"
33
34
35static void cifs_set_ops(struct inode *inode)
36{
37 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
38
39 switch (inode->i_mode & S_IFMT) {
40 case S_IFREG:
41 inode->i_op = &cifs_file_inode_ops;
42 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
43 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
44 inode->i_fop = &cifs_file_direct_nobrl_ops;
45 else
46 inode->i_fop = &cifs_file_direct_ops;
47 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
48 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
49 inode->i_fop = &cifs_file_strict_nobrl_ops;
50 else
51 inode->i_fop = &cifs_file_strict_ops;
52 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
53 inode->i_fop = &cifs_file_nobrl_ops;
54 else { /* not direct, send byte range locks */
55 inode->i_fop = &cifs_file_ops;
56 }
57
58 /* check if server can support readpages */
59 if (cifs_sb_master_tcon(cifs_sb)->ses->server->maxBuf <
60 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
61 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
62 else
63 inode->i_data.a_ops = &cifs_addr_ops;
64 break;
65 case S_IFDIR:
66#ifdef CONFIG_CIFS_DFS_UPCALL
67 if (IS_AUTOMOUNT(inode)) {
68 inode->i_op = &cifs_dfs_referral_inode_operations;
69 } else {
70#else /* NO DFS support, treat as a directory */
71 {
72#endif
73 inode->i_op = &cifs_dir_inode_ops;
74 inode->i_fop = &cifs_dir_ops;
75 }
76 break;
77 case S_IFLNK:
78 inode->i_op = &cifs_symlink_inode_ops;
79 break;
80 default:
81 init_special_inode(inode, inode->i_mode, inode->i_rdev);
82 break;
83 }
84}
85
86/* check inode attributes against fattr. If they don't match, tag the
87 * inode for cache invalidation
88 */
89static void
90cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
91{
92 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
93
94 cFYI(1, "%s: revalidating inode %llu", __func__, cifs_i->uniqueid);
95
96 if (inode->i_state & I_NEW) {
97 cFYI(1, "%s: inode %llu is new", __func__, cifs_i->uniqueid);
98 return;
99 }
100
101 /* don't bother with revalidation if we have an oplock */
102 if (cifs_i->clientCanCacheRead) {
103 cFYI(1, "%s: inode %llu is oplocked", __func__,
104 cifs_i->uniqueid);
105 return;
106 }
107
108 /* revalidate if mtime or size have changed */
109 if (timespec_equal(&inode->i_mtime, &fattr->cf_mtime) &&
110 cifs_i->server_eof == fattr->cf_eof) {
111 cFYI(1, "%s: inode %llu is unchanged", __func__,
112 cifs_i->uniqueid);
113 return;
114 }
115
116 cFYI(1, "%s: invalidating inode %llu mapping", __func__,
117 cifs_i->uniqueid);
118 cifs_i->invalid_mapping = true;
119}
120
121/* populate an inode with info from a cifs_fattr struct */
122void
123cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
124{
125 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
126 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
127 unsigned long oldtime = cifs_i->time;
128
129 cifs_revalidate_cache(inode, fattr);
130
131 inode->i_atime = fattr->cf_atime;
132 inode->i_mtime = fattr->cf_mtime;
133 inode->i_ctime = fattr->cf_ctime;
134 inode->i_rdev = fattr->cf_rdev;
135 set_nlink(inode, fattr->cf_nlink);
136 inode->i_uid = fattr->cf_uid;
137 inode->i_gid = fattr->cf_gid;
138
139 /* if dynperm is set, don't clobber existing mode */
140 if (inode->i_state & I_NEW ||
141 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
142 inode->i_mode = fattr->cf_mode;
143
144 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
145
146 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
147 cifs_i->time = 0;
148 else
149 cifs_i->time = jiffies;
150
151 cFYI(1, "inode 0x%p old_time=%ld new_time=%ld", inode,
152 oldtime, cifs_i->time);
153
154 cifs_i->delete_pending = fattr->cf_flags & CIFS_FATTR_DELETE_PENDING;
155
156 cifs_i->server_eof = fattr->cf_eof;
157 /*
158 * Can't safely change the file size here if the client is writing to
159 * it due to potential races.
160 */
161 spin_lock(&inode->i_lock);
162 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
163 i_size_write(inode, fattr->cf_eof);
164
165 /*
166 * i_blocks is not related to (i_size / i_blksize),
167 * but instead 512 byte (2**9) size is required for
168 * calculating num blocks.
169 */
170 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
171 }
172 spin_unlock(&inode->i_lock);
173
174 if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
175 inode->i_flags |= S_AUTOMOUNT;
176 if (inode->i_state & I_NEW)
177 cifs_set_ops(inode);
178}
179
180void
181cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
182{
183 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
184
185 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
186 return;
187
188 fattr->cf_uniqueid = iunique(sb, ROOT_I);
189}
190
191/* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
192void
193cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
194 struct cifs_sb_info *cifs_sb)
195{
196 memset(fattr, 0, sizeof(*fattr));
197 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
198 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
199 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
200
201 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
202 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
203 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
204 fattr->cf_mode = le64_to_cpu(info->Permissions);
205
206 /*
207 * Since we set the inode type below we need to mask off
208 * to avoid strange results if bits set above.
209 */
210 fattr->cf_mode &= ~S_IFMT;
211 switch (le32_to_cpu(info->Type)) {
212 case UNIX_FILE:
213 fattr->cf_mode |= S_IFREG;
214 fattr->cf_dtype = DT_REG;
215 break;
216 case UNIX_SYMLINK:
217 fattr->cf_mode |= S_IFLNK;
218 fattr->cf_dtype = DT_LNK;
219 break;
220 case UNIX_DIR:
221 fattr->cf_mode |= S_IFDIR;
222 fattr->cf_dtype = DT_DIR;
223 break;
224 case UNIX_CHARDEV:
225 fattr->cf_mode |= S_IFCHR;
226 fattr->cf_dtype = DT_CHR;
227 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
228 le64_to_cpu(info->DevMinor) & MINORMASK);
229 break;
230 case UNIX_BLOCKDEV:
231 fattr->cf_mode |= S_IFBLK;
232 fattr->cf_dtype = DT_BLK;
233 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
234 le64_to_cpu(info->DevMinor) & MINORMASK);
235 break;
236 case UNIX_FIFO:
237 fattr->cf_mode |= S_IFIFO;
238 fattr->cf_dtype = DT_FIFO;
239 break;
240 case UNIX_SOCKET:
241 fattr->cf_mode |= S_IFSOCK;
242 fattr->cf_dtype = DT_SOCK;
243 break;
244 default:
245 /* safest to call it a file if we do not know */
246 fattr->cf_mode |= S_IFREG;
247 fattr->cf_dtype = DT_REG;
248 cFYI(1, "unknown type %d", le32_to_cpu(info->Type));
249 break;
250 }
251
252 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
253 fattr->cf_uid = cifs_sb->mnt_uid;
254 else
255 fattr->cf_uid = le64_to_cpu(info->Uid);
256
257 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
258 fattr->cf_gid = cifs_sb->mnt_gid;
259 else
260 fattr->cf_gid = le64_to_cpu(info->Gid);
261
262 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
263}
264
265/*
266 * Fill a cifs_fattr struct with fake inode info.
267 *
268 * Needed to setup cifs_fattr data for the directory which is the
269 * junction to the new submount (ie to setup the fake directory
270 * which represents a DFS referral).
271 */
272static void
273cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
274{
275 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
276
277 cFYI(1, "creating fake fattr for DFS referral");
278
279 memset(fattr, 0, sizeof(*fattr));
280 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
281 fattr->cf_uid = cifs_sb->mnt_uid;
282 fattr->cf_gid = cifs_sb->mnt_gid;
283 fattr->cf_atime = CURRENT_TIME;
284 fattr->cf_ctime = CURRENT_TIME;
285 fattr->cf_mtime = CURRENT_TIME;
286 fattr->cf_nlink = 2;
287 fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL;
288}
289
290int cifs_get_file_info_unix(struct file *filp)
291{
292 int rc;
293 int xid;
294 FILE_UNIX_BASIC_INFO find_data;
295 struct cifs_fattr fattr;
296 struct inode *inode = filp->f_path.dentry->d_inode;
297 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
298 struct cifsFileInfo *cfile = filp->private_data;
299 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
300
301 xid = GetXid();
302 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->netfid, &find_data);
303 if (!rc) {
304 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
305 } else if (rc == -EREMOTE) {
306 cifs_create_dfs_fattr(&fattr, inode->i_sb);
307 rc = 0;
308 }
309
310 cifs_fattr_to_inode(inode, &fattr);
311 FreeXid(xid);
312 return rc;
313}
314
315int cifs_get_inode_info_unix(struct inode **pinode,
316 const unsigned char *full_path,
317 struct super_block *sb, int xid)
318{
319 int rc;
320 FILE_UNIX_BASIC_INFO find_data;
321 struct cifs_fattr fattr;
322 struct cifs_tcon *tcon;
323 struct tcon_link *tlink;
324 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
325
326 cFYI(1, "Getting info on %s", full_path);
327
328 tlink = cifs_sb_tlink(cifs_sb);
329 if (IS_ERR(tlink))
330 return PTR_ERR(tlink);
331 tcon = tlink_tcon(tlink);
332
333 /* could have done a find first instead but this returns more info */
334 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
335 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
336 CIFS_MOUNT_MAP_SPECIAL_CHR);
337 cifs_put_tlink(tlink);
338
339 if (!rc) {
340 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
341 } else if (rc == -EREMOTE) {
342 cifs_create_dfs_fattr(&fattr, sb);
343 rc = 0;
344 } else {
345 return rc;
346 }
347
348 /* check for Minshall+French symlinks */
349 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
350 int tmprc = CIFSCheckMFSymlink(&fattr, full_path, cifs_sb, xid);
351 if (tmprc)
352 cFYI(1, "CIFSCheckMFSymlink: %d", tmprc);
353 }
354
355 if (*pinode == NULL) {
356 /* get new inode */
357 cifs_fill_uniqueid(sb, &fattr);
358 *pinode = cifs_iget(sb, &fattr);
359 if (!*pinode)
360 rc = -ENOMEM;
361 } else {
362 /* we already have inode, update it */
363 cifs_fattr_to_inode(*pinode, &fattr);
364 }
365
366 return rc;
367}
368
369static int
370cifs_sfu_type(struct cifs_fattr *fattr, const unsigned char *path,
371 struct cifs_sb_info *cifs_sb, int xid)
372{
373 int rc;
374 int oplock = 0;
375 __u16 netfid;
376 struct tcon_link *tlink;
377 struct cifs_tcon *tcon;
378 struct cifs_io_parms io_parms;
379 char buf[24];
380 unsigned int bytes_read;
381 char *pbuf;
382
383 pbuf = buf;
384
385 fattr->cf_mode &= ~S_IFMT;
386
387 if (fattr->cf_eof == 0) {
388 fattr->cf_mode |= S_IFIFO;
389 fattr->cf_dtype = DT_FIFO;
390 return 0;
391 } else if (fattr->cf_eof < 8) {
392 fattr->cf_mode |= S_IFREG;
393 fattr->cf_dtype = DT_REG;
394 return -EINVAL; /* EOPNOTSUPP? */
395 }
396
397 tlink = cifs_sb_tlink(cifs_sb);
398 if (IS_ERR(tlink))
399 return PTR_ERR(tlink);
400 tcon = tlink_tcon(tlink);
401
402 rc = CIFSSMBOpen(xid, tcon, path, FILE_OPEN, GENERIC_READ,
403 CREATE_NOT_DIR, &netfid, &oplock, NULL,
404 cifs_sb->local_nls,
405 cifs_sb->mnt_cifs_flags &
406 CIFS_MOUNT_MAP_SPECIAL_CHR);
407 if (rc == 0) {
408 int buf_type = CIFS_NO_BUFFER;
409 /* Read header */
410 io_parms.netfid = netfid;
411 io_parms.pid = current->tgid;
412 io_parms.tcon = tcon;
413 io_parms.offset = 0;
414 io_parms.length = 24;
415 rc = CIFSSMBRead(xid, &io_parms, &bytes_read, &pbuf,
416 &buf_type);
417 if ((rc == 0) && (bytes_read >= 8)) {
418 if (memcmp("IntxBLK", pbuf, 8) == 0) {
419 cFYI(1, "Block device");
420 fattr->cf_mode |= S_IFBLK;
421 fattr->cf_dtype = DT_BLK;
422 if (bytes_read == 24) {
423 /* we have enough to decode dev num */
424 __u64 mjr; /* major */
425 __u64 mnr; /* minor */
426 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
427 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
428 fattr->cf_rdev = MKDEV(mjr, mnr);
429 }
430 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
431 cFYI(1, "Char device");
432 fattr->cf_mode |= S_IFCHR;
433 fattr->cf_dtype = DT_CHR;
434 if (bytes_read == 24) {
435 /* we have enough to decode dev num */
436 __u64 mjr; /* major */
437 __u64 mnr; /* minor */
438 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
439 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
440 fattr->cf_rdev = MKDEV(mjr, mnr);
441 }
442 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
443 cFYI(1, "Symlink");
444 fattr->cf_mode |= S_IFLNK;
445 fattr->cf_dtype = DT_LNK;
446 } else {
447 fattr->cf_mode |= S_IFREG; /* file? */
448 fattr->cf_dtype = DT_REG;
449 rc = -EOPNOTSUPP;
450 }
451 } else {
452 fattr->cf_mode |= S_IFREG; /* then it is a file */
453 fattr->cf_dtype = DT_REG;
454 rc = -EOPNOTSUPP; /* or some unknown SFU type */
455 }
456 CIFSSMBClose(xid, tcon, netfid);
457 }
458 cifs_put_tlink(tlink);
459 return rc;
460}
461
462#define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
463
464/*
465 * Fetch mode bits as provided by SFU.
466 *
467 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
468 */
469static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
470 struct cifs_sb_info *cifs_sb, int xid)
471{
472#ifdef CONFIG_CIFS_XATTR
473 ssize_t rc;
474 char ea_value[4];
475 __u32 mode;
476 struct tcon_link *tlink;
477 struct cifs_tcon *tcon;
478
479 tlink = cifs_sb_tlink(cifs_sb);
480 if (IS_ERR(tlink))
481 return PTR_ERR(tlink);
482 tcon = tlink_tcon(tlink);
483
484 rc = CIFSSMBQAllEAs(xid, tcon, path, "SETFILEBITS",
485 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
486 cifs_sb->mnt_cifs_flags &
487 CIFS_MOUNT_MAP_SPECIAL_CHR);
488 cifs_put_tlink(tlink);
489 if (rc < 0)
490 return (int)rc;
491 else if (rc > 3) {
492 mode = le32_to_cpu(*((__le32 *)ea_value));
493 fattr->cf_mode &= ~SFBITS_MASK;
494 cFYI(1, "special bits 0%o org mode 0%o", mode,
495 fattr->cf_mode);
496 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
497 cFYI(1, "special mode bits 0%o", mode);
498 }
499
500 return 0;
501#else
502 return -EOPNOTSUPP;
503#endif
504}
505
506/* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
507static void
508cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
509 struct cifs_sb_info *cifs_sb, bool adjust_tz)
510{
511 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
512
513 memset(fattr, 0, sizeof(*fattr));
514 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
515 if (info->DeletePending)
516 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
517
518 if (info->LastAccessTime)
519 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
520 else
521 fattr->cf_atime = CURRENT_TIME;
522
523 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
524 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
525
526 if (adjust_tz) {
527 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
528 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
529 }
530
531 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
532 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
533 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
534
535 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
536 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
537 fattr->cf_dtype = DT_DIR;
538 /*
539 * Server can return wrong NumberOfLinks value for directories
540 * when Unix extensions are disabled - fake it.
541 */
542 fattr->cf_nlink = 2;
543 } else {
544 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
545 fattr->cf_dtype = DT_REG;
546
547 /* clear write bits if ATTR_READONLY is set */
548 if (fattr->cf_cifsattrs & ATTR_READONLY)
549 fattr->cf_mode &= ~(S_IWUGO);
550
551 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
552 if (fattr->cf_nlink < 1) {
553 cFYI(1, "replacing bogus file nlink value %u\n",
554 fattr->cf_nlink);
555 fattr->cf_nlink = 1;
556 }
557 }
558
559 fattr->cf_uid = cifs_sb->mnt_uid;
560 fattr->cf_gid = cifs_sb->mnt_gid;
561}
562
563int cifs_get_file_info(struct file *filp)
564{
565 int rc;
566 int xid;
567 FILE_ALL_INFO find_data;
568 struct cifs_fattr fattr;
569 struct inode *inode = filp->f_path.dentry->d_inode;
570 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
571 struct cifsFileInfo *cfile = filp->private_data;
572 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
573
574 xid = GetXid();
575 rc = CIFSSMBQFileInfo(xid, tcon, cfile->netfid, &find_data);
576 switch (rc) {
577 case 0:
578 cifs_all_info_to_fattr(&fattr, &find_data, cifs_sb, false);
579 break;
580 case -EREMOTE:
581 cifs_create_dfs_fattr(&fattr, inode->i_sb);
582 rc = 0;
583 break;
584 case -EOPNOTSUPP:
585 case -EINVAL:
586 /*
587 * FIXME: legacy server -- fall back to path-based call?
588 * for now, just skip revalidating and mark inode for
589 * immediate reval.
590 */
591 rc = 0;
592 CIFS_I(inode)->time = 0;
593 default:
594 goto cgfi_exit;
595 }
596
597 /*
598 * don't bother with SFU junk here -- just mark inode as needing
599 * revalidation.
600 */
601 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
602 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
603 cifs_fattr_to_inode(inode, &fattr);
604cgfi_exit:
605 FreeXid(xid);
606 return rc;
607}
608
609int cifs_get_inode_info(struct inode **pinode,
610 const unsigned char *full_path, FILE_ALL_INFO *pfindData,
611 struct super_block *sb, int xid, const __u16 *pfid)
612{
613 int rc = 0, tmprc;
614 struct cifs_tcon *pTcon;
615 struct tcon_link *tlink;
616 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
617 char *buf = NULL;
618 bool adjustTZ = false;
619 struct cifs_fattr fattr;
620
621 tlink = cifs_sb_tlink(cifs_sb);
622 if (IS_ERR(tlink))
623 return PTR_ERR(tlink);
624 pTcon = tlink_tcon(tlink);
625
626 cFYI(1, "Getting info on %s", full_path);
627
628 if ((pfindData == NULL) && (*pinode != NULL)) {
629 if (CIFS_I(*pinode)->clientCanCacheRead) {
630 cFYI(1, "No need to revalidate cached inode sizes");
631 goto cgii_exit;
632 }
633 }
634
635 /* if file info not passed in then get it from server */
636 if (pfindData == NULL) {
637 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
638 if (buf == NULL) {
639 rc = -ENOMEM;
640 goto cgii_exit;
641 }
642 pfindData = (FILE_ALL_INFO *)buf;
643
644 /* could do find first instead but this returns more info */
645 rc = CIFSSMBQPathInfo(xid, pTcon, full_path, pfindData,
646 0 /* not legacy */,
647 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
648 CIFS_MOUNT_MAP_SPECIAL_CHR);
649 /* BB optimize code so we do not make the above call
650 when server claims no NT SMB support and the above call
651 failed at least once - set flag in tcon or mount */
652 if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
653 rc = SMBQueryInformation(xid, pTcon, full_path,
654 pfindData, cifs_sb->local_nls,
655 cifs_sb->mnt_cifs_flags &
656 CIFS_MOUNT_MAP_SPECIAL_CHR);
657 adjustTZ = true;
658 }
659 }
660
661 if (!rc) {
662 cifs_all_info_to_fattr(&fattr, (FILE_ALL_INFO *) pfindData,
663 cifs_sb, adjustTZ);
664 } else if (rc == -EREMOTE) {
665 cifs_create_dfs_fattr(&fattr, sb);
666 rc = 0;
667 } else {
668 goto cgii_exit;
669 }
670
671 /*
672 * If an inode wasn't passed in, then get the inode number
673 *
674 * Is an i_ino of zero legal? Can we use that to check if the server
675 * supports returning inode numbers? Are there other sanity checks we
676 * can use to ensure that the server is really filling in that field?
677 *
678 * We can not use the IndexNumber field by default from Windows or
679 * Samba (in ALL_INFO buf) but we can request it explicitly. The SNIA
680 * CIFS spec claims that this value is unique within the scope of a
681 * share, and the windows docs hint that it's actually unique
682 * per-machine.
683 *
684 * There may be higher info levels that work but are there Windows
685 * server or network appliances for which IndexNumber field is not
686 * guaranteed unique?
687 */
688 if (*pinode == NULL) {
689 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
690 int rc1 = 0;
691
692 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
693 full_path, &fattr.cf_uniqueid,
694 cifs_sb->local_nls,
695 cifs_sb->mnt_cifs_flags &
696 CIFS_MOUNT_MAP_SPECIAL_CHR);
697 if (rc1 || !fattr.cf_uniqueid) {
698 cFYI(1, "GetSrvInodeNum rc %d", rc1);
699 fattr.cf_uniqueid = iunique(sb, ROOT_I);
700 cifs_autodisable_serverino(cifs_sb);
701 }
702 } else {
703 fattr.cf_uniqueid = iunique(sb, ROOT_I);
704 }
705 } else {
706 fattr.cf_uniqueid = CIFS_I(*pinode)->uniqueid;
707 }
708
709 /* query for SFU type info if supported and needed */
710 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
711 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
712 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
713 if (tmprc)
714 cFYI(1, "cifs_sfu_type failed: %d", tmprc);
715 }
716
717#ifdef CONFIG_CIFS_ACL
718 /* fill in 0777 bits from ACL */
719 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
720 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *pinode, full_path,
721 pfid);
722 if (rc) {
723 cFYI(1, "%s: Getting ACL failed with error: %d",
724 __func__, rc);
725 goto cgii_exit;
726 }
727 }
728#endif /* CONFIG_CIFS_ACL */
729
730 /* fill in remaining high mode bits e.g. SUID, VTX */
731 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
732 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
733
734 /* check for Minshall+French symlinks */
735 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
736 tmprc = CIFSCheckMFSymlink(&fattr, full_path, cifs_sb, xid);
737 if (tmprc)
738 cFYI(1, "CIFSCheckMFSymlink: %d", tmprc);
739 }
740
741 if (!*pinode) {
742 *pinode = cifs_iget(sb, &fattr);
743 if (!*pinode)
744 rc = -ENOMEM;
745 } else {
746 cifs_fattr_to_inode(*pinode, &fattr);
747 }
748
749cgii_exit:
750 kfree(buf);
751 cifs_put_tlink(tlink);
752 return rc;
753}
754
755static const struct inode_operations cifs_ipc_inode_ops = {
756 .lookup = cifs_lookup,
757};
758
759char *cifs_build_path_to_root(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
760 struct cifs_tcon *tcon)
761{
762 int pplen = vol->prepath ? strlen(vol->prepath) : 0;
763 int dfsplen;
764 char *full_path = NULL;
765
766 /* if no prefix path, simply set path to the root of share to "" */
767 if (pplen == 0) {
768 full_path = kmalloc(1, GFP_KERNEL);
769 if (full_path)
770 full_path[0] = 0;
771 return full_path;
772 }
773
774 if (tcon->Flags & SMB_SHARE_IS_IN_DFS)
775 dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
776 else
777 dfsplen = 0;
778
779 full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
780 if (full_path == NULL)
781 return full_path;
782
783 if (dfsplen)
784 strncpy(full_path, tcon->treeName, dfsplen);
785 strncpy(full_path + dfsplen, vol->prepath, pplen);
786 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
787 full_path[dfsplen + pplen] = 0; /* add trailing null */
788 return full_path;
789}
790
791static int
792cifs_find_inode(struct inode *inode, void *opaque)
793{
794 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
795
796 /* don't match inode with different uniqueid */
797 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
798 return 0;
799
800 /* use createtime like an i_generation field */
801 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
802 return 0;
803
804 /* don't match inode of different type */
805 if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
806 return 0;
807
808 /* if it's not a directory or has no dentries, then flag it */
809 if (S_ISDIR(inode->i_mode) && !list_empty(&inode->i_dentry))
810 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
811
812 return 1;
813}
814
815static int
816cifs_init_inode(struct inode *inode, void *opaque)
817{
818 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
819
820 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
821 CIFS_I(inode)->createtime = fattr->cf_createtime;
822 return 0;
823}
824
825/*
826 * walk dentry list for an inode and report whether it has aliases that
827 * are hashed. We use this to determine if a directory inode can actually
828 * be used.
829 */
830static bool
831inode_has_hashed_dentries(struct inode *inode)
832{
833 struct dentry *dentry;
834
835 spin_lock(&inode->i_lock);
836 list_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
837 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
838 spin_unlock(&inode->i_lock);
839 return true;
840 }
841 }
842 spin_unlock(&inode->i_lock);
843 return false;
844}
845
846/* Given fattrs, get a corresponding inode */
847struct inode *
848cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
849{
850 unsigned long hash;
851 struct inode *inode;
852
853retry_iget5_locked:
854 cFYI(1, "looking for uniqueid=%llu", fattr->cf_uniqueid);
855
856 /* hash down to 32-bits on 32-bit arch */
857 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
858
859 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
860 if (inode) {
861 /* was there a potentially problematic inode collision? */
862 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
863 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
864
865 if (inode_has_hashed_dentries(inode)) {
866 cifs_autodisable_serverino(CIFS_SB(sb));
867 iput(inode);
868 fattr->cf_uniqueid = iunique(sb, ROOT_I);
869 goto retry_iget5_locked;
870 }
871 }
872
873 cifs_fattr_to_inode(inode, fattr);
874 if (sb->s_flags & MS_NOATIME)
875 inode->i_flags |= S_NOATIME | S_NOCMTIME;
876 if (inode->i_state & I_NEW) {
877 inode->i_ino = hash;
878 if (S_ISREG(inode->i_mode))
879 inode->i_data.backing_dev_info = sb->s_bdi;
880#ifdef CONFIG_CIFS_FSCACHE
881 /* initialize per-inode cache cookie pointer */
882 CIFS_I(inode)->fscache = NULL;
883#endif
884 unlock_new_inode(inode);
885 }
886 }
887
888 return inode;
889}
890
891/* gets root inode */
892struct inode *cifs_root_iget(struct super_block *sb)
893{
894 int xid;
895 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
896 struct inode *inode = NULL;
897 long rc;
898 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
899
900 xid = GetXid();
901 if (tcon->unix_ext)
902 rc = cifs_get_inode_info_unix(&inode, "", sb, xid);
903 else
904 rc = cifs_get_inode_info(&inode, "", NULL, sb, xid, NULL);
905
906 if (!inode) {
907 inode = ERR_PTR(rc);
908 goto out;
909 }
910
911#ifdef CONFIG_CIFS_FSCACHE
912 /* populate tcon->resource_id */
913 tcon->resource_id = CIFS_I(inode)->uniqueid;
914#endif
915
916 if (rc && tcon->ipc) {
917 cFYI(1, "ipc connection - fake read inode");
918 inode->i_mode |= S_IFDIR;
919 set_nlink(inode, 2);
920 inode->i_op = &cifs_ipc_inode_ops;
921 inode->i_fop = &simple_dir_operations;
922 inode->i_uid = cifs_sb->mnt_uid;
923 inode->i_gid = cifs_sb->mnt_gid;
924 } else if (rc) {
925 iget_failed(inode);
926 inode = ERR_PTR(rc);
927 }
928
929out:
930 /* can not call macro FreeXid here since in a void func
931 * TODO: This is no longer true
932 */
933 _FreeXid(xid);
934 return inode;
935}
936
937static int
938cifs_set_file_info(struct inode *inode, struct iattr *attrs, int xid,
939 char *full_path, __u32 dosattr)
940{
941 int rc;
942 int oplock = 0;
943 __u16 netfid;
944 __u32 netpid;
945 bool set_time = false;
946 struct cifsFileInfo *open_file;
947 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
948 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
949 struct tcon_link *tlink = NULL;
950 struct cifs_tcon *pTcon;
951 FILE_BASIC_INFO info_buf;
952
953 if (attrs == NULL)
954 return -EINVAL;
955
956 if (attrs->ia_valid & ATTR_ATIME) {
957 set_time = true;
958 info_buf.LastAccessTime =
959 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
960 } else
961 info_buf.LastAccessTime = 0;
962
963 if (attrs->ia_valid & ATTR_MTIME) {
964 set_time = true;
965 info_buf.LastWriteTime =
966 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
967 } else
968 info_buf.LastWriteTime = 0;
969
970 /*
971 * Samba throws this field away, but windows may actually use it.
972 * Do not set ctime unless other time stamps are changed explicitly
973 * (i.e. by utimes()) since we would then have a mix of client and
974 * server times.
975 */
976 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
977 cFYI(1, "CIFS - CTIME changed");
978 info_buf.ChangeTime =
979 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
980 } else
981 info_buf.ChangeTime = 0;
982
983 info_buf.CreationTime = 0; /* don't change */
984 info_buf.Attributes = cpu_to_le32(dosattr);
985
986 /*
987 * If the file is already open for write, just use that fileid
988 */
989 open_file = find_writable_file(cifsInode, true);
990 if (open_file) {
991 netfid = open_file->netfid;
992 netpid = open_file->pid;
993 pTcon = tlink_tcon(open_file->tlink);
994 goto set_via_filehandle;
995 }
996
997 tlink = cifs_sb_tlink(cifs_sb);
998 if (IS_ERR(tlink)) {
999 rc = PTR_ERR(tlink);
1000 tlink = NULL;
1001 goto out;
1002 }
1003 pTcon = tlink_tcon(tlink);
1004
1005 /*
1006 * NT4 apparently returns success on this call, but it doesn't
1007 * really work.
1008 */
1009 if (!(pTcon->ses->flags & CIFS_SES_NT4)) {
1010 rc = CIFSSMBSetPathInfo(xid, pTcon, full_path,
1011 &info_buf, cifs_sb->local_nls,
1012 cifs_sb->mnt_cifs_flags &
1013 CIFS_MOUNT_MAP_SPECIAL_CHR);
1014 if (rc == 0) {
1015 cifsInode->cifsAttrs = dosattr;
1016 goto out;
1017 } else if (rc != -EOPNOTSUPP && rc != -EINVAL)
1018 goto out;
1019 }
1020
1021 cFYI(1, "calling SetFileInfo since SetPathInfo for "
1022 "times not supported by this server");
1023 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
1024 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1025 CREATE_NOT_DIR, &netfid, &oplock,
1026 NULL, cifs_sb->local_nls,
1027 cifs_sb->mnt_cifs_flags &
1028 CIFS_MOUNT_MAP_SPECIAL_CHR);
1029
1030 if (rc != 0) {
1031 if (rc == -EIO)
1032 rc = -EINVAL;
1033 goto out;
1034 }
1035
1036 netpid = current->tgid;
1037
1038set_via_filehandle:
1039 rc = CIFSSMBSetFileInfo(xid, pTcon, &info_buf, netfid, netpid);
1040 if (!rc)
1041 cifsInode->cifsAttrs = dosattr;
1042
1043 if (open_file == NULL)
1044 CIFSSMBClose(xid, pTcon, netfid);
1045 else
1046 cifsFileInfo_put(open_file);
1047out:
1048 if (tlink != NULL)
1049 cifs_put_tlink(tlink);
1050 return rc;
1051}
1052
1053/*
1054 * open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1055 * and rename it to a random name that hopefully won't conflict with
1056 * anything else.
1057 */
1058static int
1059cifs_rename_pending_delete(char *full_path, struct dentry *dentry, int xid)
1060{
1061 int oplock = 0;
1062 int rc;
1063 __u16 netfid;
1064 struct inode *inode = dentry->d_inode;
1065 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1066 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1067 struct tcon_link *tlink;
1068 struct cifs_tcon *tcon;
1069 __u32 dosattr, origattr;
1070 FILE_BASIC_INFO *info_buf = NULL;
1071
1072 tlink = cifs_sb_tlink(cifs_sb);
1073 if (IS_ERR(tlink))
1074 return PTR_ERR(tlink);
1075 tcon = tlink_tcon(tlink);
1076
1077 rc = CIFSSMBOpen(xid, tcon, full_path, FILE_OPEN,
1078 DELETE|FILE_WRITE_ATTRIBUTES, CREATE_NOT_DIR,
1079 &netfid, &oplock, NULL, cifs_sb->local_nls,
1080 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1081 if (rc != 0)
1082 goto out;
1083
1084 origattr = cifsInode->cifsAttrs;
1085 if (origattr == 0)
1086 origattr |= ATTR_NORMAL;
1087
1088 dosattr = origattr & ~ATTR_READONLY;
1089 if (dosattr == 0)
1090 dosattr |= ATTR_NORMAL;
1091 dosattr |= ATTR_HIDDEN;
1092
1093 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1094 if (dosattr != origattr) {
1095 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1096 if (info_buf == NULL) {
1097 rc = -ENOMEM;
1098 goto out_close;
1099 }
1100 info_buf->Attributes = cpu_to_le32(dosattr);
1101 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
1102 current->tgid);
1103 /* although we would like to mark the file hidden
1104 if that fails we will still try to rename it */
1105 if (rc != 0)
1106 cifsInode->cifsAttrs = dosattr;
1107 else
1108 dosattr = origattr; /* since not able to change them */
1109 }
1110
1111 /* rename the file */
1112 rc = CIFSSMBRenameOpenFile(xid, tcon, netfid, NULL, cifs_sb->local_nls,
1113 cifs_sb->mnt_cifs_flags &
1114 CIFS_MOUNT_MAP_SPECIAL_CHR);
1115 if (rc != 0) {
1116 rc = -ETXTBSY;
1117 goto undo_setattr;
1118 }
1119
1120 /* try to set DELETE_ON_CLOSE */
1121 if (!cifsInode->delete_pending) {
1122 rc = CIFSSMBSetFileDisposition(xid, tcon, true, netfid,
1123 current->tgid);
1124 /*
1125 * some samba versions return -ENOENT when we try to set the
1126 * file disposition here. Likely a samba bug, but work around
1127 * it for now. This means that some cifsXXX files may hang
1128 * around after they shouldn't.
1129 *
1130 * BB: remove this hack after more servers have the fix
1131 */
1132 if (rc == -ENOENT)
1133 rc = 0;
1134 else if (rc != 0) {
1135 rc = -ETXTBSY;
1136 goto undo_rename;
1137 }
1138 cifsInode->delete_pending = true;
1139 }
1140
1141out_close:
1142 CIFSSMBClose(xid, tcon, netfid);
1143out:
1144 kfree(info_buf);
1145 cifs_put_tlink(tlink);
1146 return rc;
1147
1148 /*
1149 * reset everything back to the original state. Don't bother
1150 * dealing with errors here since we can't do anything about
1151 * them anyway.
1152 */
1153undo_rename:
1154 CIFSSMBRenameOpenFile(xid, tcon, netfid, dentry->d_name.name,
1155 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1156 CIFS_MOUNT_MAP_SPECIAL_CHR);
1157undo_setattr:
1158 if (dosattr != origattr) {
1159 info_buf->Attributes = cpu_to_le32(origattr);
1160 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
1161 current->tgid))
1162 cifsInode->cifsAttrs = origattr;
1163 }
1164
1165 goto out_close;
1166}
1167
1168
1169/*
1170 * If dentry->d_inode is null (usually meaning the cached dentry
1171 * is a negative dentry) then we would attempt a standard SMB delete, but
1172 * if that fails we can not attempt the fall back mechanisms on EACCESS
1173 * but will return the EACCESS to the caller. Note that the VFS does not call
1174 * unlink on negative dentries currently.
1175 */
1176int cifs_unlink(struct inode *dir, struct dentry *dentry)
1177{
1178 int rc = 0;
1179 int xid;
1180 char *full_path = NULL;
1181 struct inode *inode = dentry->d_inode;
1182 struct cifsInodeInfo *cifs_inode;
1183 struct super_block *sb = dir->i_sb;
1184 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1185 struct tcon_link *tlink;
1186 struct cifs_tcon *tcon;
1187 struct iattr *attrs = NULL;
1188 __u32 dosattr = 0, origattr = 0;
1189
1190 cFYI(1, "cifs_unlink, dir=0x%p, dentry=0x%p", dir, dentry);
1191
1192 tlink = cifs_sb_tlink(cifs_sb);
1193 if (IS_ERR(tlink))
1194 return PTR_ERR(tlink);
1195 tcon = tlink_tcon(tlink);
1196
1197 xid = GetXid();
1198
1199 /* Unlink can be called from rename so we can not take the
1200 * sb->s_vfs_rename_mutex here */
1201 full_path = build_path_from_dentry(dentry);
1202 if (full_path == NULL) {
1203 rc = -ENOMEM;
1204 goto unlink_out;
1205 }
1206
1207 if ((tcon->ses->capabilities & CAP_UNIX) &&
1208 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1209 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1210 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1211 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1212 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1213 cFYI(1, "posix del rc %d", rc);
1214 if ((rc == 0) || (rc == -ENOENT))
1215 goto psx_del_no_retry;
1216 }
1217
1218retry_std_delete:
1219 rc = CIFSSMBDelFile(xid, tcon, full_path, cifs_sb->local_nls,
1220 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1221
1222psx_del_no_retry:
1223 if (!rc) {
1224 if (inode)
1225 drop_nlink(inode);
1226 } else if (rc == -ENOENT) {
1227 d_drop(dentry);
1228 } else if (rc == -ETXTBSY) {
1229 rc = cifs_rename_pending_delete(full_path, dentry, xid);
1230 if (rc == 0)
1231 drop_nlink(inode);
1232 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1233 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1234 if (attrs == NULL) {
1235 rc = -ENOMEM;
1236 goto out_reval;
1237 }
1238
1239 /* try to reset dos attributes */
1240 cifs_inode = CIFS_I(inode);
1241 origattr = cifs_inode->cifsAttrs;
1242 if (origattr == 0)
1243 origattr |= ATTR_NORMAL;
1244 dosattr = origattr & ~ATTR_READONLY;
1245 if (dosattr == 0)
1246 dosattr |= ATTR_NORMAL;
1247 dosattr |= ATTR_HIDDEN;
1248
1249 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1250 if (rc != 0)
1251 goto out_reval;
1252
1253 goto retry_std_delete;
1254 }
1255
1256 /* undo the setattr if we errored out and it's needed */
1257 if (rc != 0 && dosattr != 0)
1258 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1259
1260out_reval:
1261 if (inode) {
1262 cifs_inode = CIFS_I(inode);
1263 cifs_inode->time = 0; /* will force revalidate to get info
1264 when needed */
1265 inode->i_ctime = current_fs_time(sb);
1266 }
1267 dir->i_ctime = dir->i_mtime = current_fs_time(sb);
1268 cifs_inode = CIFS_I(dir);
1269 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1270unlink_out:
1271 kfree(full_path);
1272 kfree(attrs);
1273 FreeXid(xid);
1274 cifs_put_tlink(tlink);
1275 return rc;
1276}
1277
1278int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
1279{
1280 int rc = 0, tmprc;
1281 int xid;
1282 struct cifs_sb_info *cifs_sb;
1283 struct tcon_link *tlink;
1284 struct cifs_tcon *pTcon;
1285 char *full_path = NULL;
1286 struct inode *newinode = NULL;
1287 struct cifs_fattr fattr;
1288
1289 cFYI(1, "In cifs_mkdir, mode = 0x%hx inode = 0x%p", mode, inode);
1290
1291 cifs_sb = CIFS_SB(inode->i_sb);
1292 tlink = cifs_sb_tlink(cifs_sb);
1293 if (IS_ERR(tlink))
1294 return PTR_ERR(tlink);
1295 pTcon = tlink_tcon(tlink);
1296
1297 xid = GetXid();
1298
1299 full_path = build_path_from_dentry(direntry);
1300 if (full_path == NULL) {
1301 rc = -ENOMEM;
1302 goto mkdir_out;
1303 }
1304
1305 if ((pTcon->ses->capabilities & CAP_UNIX) &&
1306 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1307 le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
1308 u32 oplock = 0;
1309 FILE_UNIX_BASIC_INFO *pInfo =
1310 kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1311 if (pInfo == NULL) {
1312 rc = -ENOMEM;
1313 goto mkdir_out;
1314 }
1315
1316 mode &= ~current_umask();
1317 rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT,
1318 mode, NULL /* netfid */, pInfo, &oplock,
1319 full_path, cifs_sb->local_nls,
1320 cifs_sb->mnt_cifs_flags &
1321 CIFS_MOUNT_MAP_SPECIAL_CHR);
1322 if (rc == -EOPNOTSUPP) {
1323 kfree(pInfo);
1324 goto mkdir_retry_old;
1325 } else if (rc) {
1326 cFYI(1, "posix mkdir returned 0x%x", rc);
1327 d_drop(direntry);
1328 } else {
1329 if (pInfo->Type == cpu_to_le32(-1)) {
1330 /* no return info, go query for it */
1331 kfree(pInfo);
1332 goto mkdir_get_info;
1333 }
1334/*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need
1335 to set uid/gid */
1336
1337 cifs_unix_basic_to_fattr(&fattr, pInfo, cifs_sb);
1338 cifs_fill_uniqueid(inode->i_sb, &fattr);
1339 newinode = cifs_iget(inode->i_sb, &fattr);
1340 if (!newinode) {
1341 kfree(pInfo);
1342 goto mkdir_get_info;
1343 }
1344
1345 d_instantiate(direntry, newinode);
1346
1347#ifdef CONFIG_CIFS_DEBUG2
1348 cFYI(1, "instantiated dentry %p %s to inode %p",
1349 direntry, direntry->d_name.name, newinode);
1350
1351 if (newinode->i_nlink != 2)
1352 cFYI(1, "unexpected number of links %d",
1353 newinode->i_nlink);
1354#endif
1355 }
1356 kfree(pInfo);
1357 goto mkdir_out;
1358 }
1359mkdir_retry_old:
1360 /* BB add setting the equivalent of mode via CreateX w/ACLs */
1361 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
1362 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1363 if (rc) {
1364 cFYI(1, "cifs_mkdir returned 0x%x", rc);
1365 d_drop(direntry);
1366 } else {
1367mkdir_get_info:
1368 if (pTcon->unix_ext)
1369 rc = cifs_get_inode_info_unix(&newinode, full_path,
1370 inode->i_sb, xid);
1371 else
1372 rc = cifs_get_inode_info(&newinode, full_path, NULL,
1373 inode->i_sb, xid, NULL);
1374
1375 d_instantiate(direntry, newinode);
1376 /* setting nlink not necessary except in cases where we
1377 * failed to get it from the server or was set bogus */
1378 if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2))
1379 set_nlink(direntry->d_inode, 2);
1380
1381 mode &= ~current_umask();
1382 /* must turn on setgid bit if parent dir has it */
1383 if (inode->i_mode & S_ISGID)
1384 mode |= S_ISGID;
1385
1386 if (pTcon->unix_ext) {
1387 struct cifs_unix_set_info_args args = {
1388 .mode = mode,
1389 .ctime = NO_CHANGE_64,
1390 .atime = NO_CHANGE_64,
1391 .mtime = NO_CHANGE_64,
1392 .device = 0,
1393 };
1394 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1395 args.uid = (__u64)current_fsuid();
1396 if (inode->i_mode & S_ISGID)
1397 args.gid = (__u64)inode->i_gid;
1398 else
1399 args.gid = (__u64)current_fsgid();
1400 } else {
1401 args.uid = NO_CHANGE_64;
1402 args.gid = NO_CHANGE_64;
1403 }
1404 CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, &args,
1405 cifs_sb->local_nls,
1406 cifs_sb->mnt_cifs_flags &
1407 CIFS_MOUNT_MAP_SPECIAL_CHR);
1408 } else {
1409 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1410 (mode & S_IWUGO) == 0) {
1411 FILE_BASIC_INFO pInfo;
1412 struct cifsInodeInfo *cifsInode;
1413 u32 dosattrs;
1414
1415 memset(&pInfo, 0, sizeof(pInfo));
1416 cifsInode = CIFS_I(newinode);
1417 dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
1418 pInfo.Attributes = cpu_to_le32(dosattrs);
1419 tmprc = CIFSSMBSetPathInfo(xid, pTcon,
1420 full_path, &pInfo,
1421 cifs_sb->local_nls,
1422 cifs_sb->mnt_cifs_flags &
1423 CIFS_MOUNT_MAP_SPECIAL_CHR);
1424 if (tmprc == 0)
1425 cifsInode->cifsAttrs = dosattrs;
1426 }
1427 if (direntry->d_inode) {
1428 if (cifs_sb->mnt_cifs_flags &
1429 CIFS_MOUNT_DYNPERM)
1430 direntry->d_inode->i_mode =
1431 (mode | S_IFDIR);
1432
1433 if (cifs_sb->mnt_cifs_flags &
1434 CIFS_MOUNT_SET_UID) {
1435 direntry->d_inode->i_uid =
1436 current_fsuid();
1437 if (inode->i_mode & S_ISGID)
1438 direntry->d_inode->i_gid =
1439 inode->i_gid;
1440 else
1441 direntry->d_inode->i_gid =
1442 current_fsgid();
1443 }
1444 }
1445 }
1446 }
1447mkdir_out:
1448 /*
1449 * Force revalidate to get parent dir info when needed since cached
1450 * attributes are invalid now.
1451 */
1452 CIFS_I(inode)->time = 0;
1453 kfree(full_path);
1454 FreeXid(xid);
1455 cifs_put_tlink(tlink);
1456 return rc;
1457}
1458
1459int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1460{
1461 int rc = 0;
1462 int xid;
1463 struct cifs_sb_info *cifs_sb;
1464 struct tcon_link *tlink;
1465 struct cifs_tcon *pTcon;
1466 char *full_path = NULL;
1467 struct cifsInodeInfo *cifsInode;
1468
1469 cFYI(1, "cifs_rmdir, inode = 0x%p", inode);
1470
1471 xid = GetXid();
1472
1473 full_path = build_path_from_dentry(direntry);
1474 if (full_path == NULL) {
1475 rc = -ENOMEM;
1476 goto rmdir_exit;
1477 }
1478
1479 cifs_sb = CIFS_SB(inode->i_sb);
1480 tlink = cifs_sb_tlink(cifs_sb);
1481 if (IS_ERR(tlink)) {
1482 rc = PTR_ERR(tlink);
1483 goto rmdir_exit;
1484 }
1485 pTcon = tlink_tcon(tlink);
1486
1487 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
1488 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1489 cifs_put_tlink(tlink);
1490
1491 if (!rc) {
1492 spin_lock(&direntry->d_inode->i_lock);
1493 i_size_write(direntry->d_inode, 0);
1494 clear_nlink(direntry->d_inode);
1495 spin_unlock(&direntry->d_inode->i_lock);
1496 }
1497
1498 cifsInode = CIFS_I(direntry->d_inode);
1499 /* force revalidate to go get info when needed */
1500 cifsInode->time = 0;
1501
1502 cifsInode = CIFS_I(inode);
1503 /*
1504 * Force revalidate to get parent dir info when needed since cached
1505 * attributes are invalid now.
1506 */
1507 cifsInode->time = 0;
1508
1509 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
1510 current_fs_time(inode->i_sb);
1511
1512rmdir_exit:
1513 kfree(full_path);
1514 FreeXid(xid);
1515 return rc;
1516}
1517
1518static int
1519cifs_do_rename(int xid, struct dentry *from_dentry, const char *fromPath,
1520 struct dentry *to_dentry, const char *toPath)
1521{
1522 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
1523 struct tcon_link *tlink;
1524 struct cifs_tcon *pTcon;
1525 __u16 srcfid;
1526 int oplock, rc;
1527
1528 tlink = cifs_sb_tlink(cifs_sb);
1529 if (IS_ERR(tlink))
1530 return PTR_ERR(tlink);
1531 pTcon = tlink_tcon(tlink);
1532
1533 /* try path-based rename first */
1534 rc = CIFSSMBRename(xid, pTcon, fromPath, toPath, cifs_sb->local_nls,
1535 cifs_sb->mnt_cifs_flags &
1536 CIFS_MOUNT_MAP_SPECIAL_CHR);
1537
1538 /*
1539 * don't bother with rename by filehandle unless file is busy and
1540 * source Note that cross directory moves do not work with
1541 * rename by filehandle to various Windows servers.
1542 */
1543 if (rc == 0 || rc != -ETXTBSY)
1544 goto do_rename_exit;
1545
1546 /* open-file renames don't work across directories */
1547 if (to_dentry->d_parent != from_dentry->d_parent)
1548 goto do_rename_exit;
1549
1550 /* open the file to be renamed -- we need DELETE perms */
1551 rc = CIFSSMBOpen(xid, pTcon, fromPath, FILE_OPEN, DELETE,
1552 CREATE_NOT_DIR, &srcfid, &oplock, NULL,
1553 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1554 CIFS_MOUNT_MAP_SPECIAL_CHR);
1555
1556 if (rc == 0) {
1557 rc = CIFSSMBRenameOpenFile(xid, pTcon, srcfid,
1558 (const char *) to_dentry->d_name.name,
1559 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1560 CIFS_MOUNT_MAP_SPECIAL_CHR);
1561
1562 CIFSSMBClose(xid, pTcon, srcfid);
1563 }
1564do_rename_exit:
1565 cifs_put_tlink(tlink);
1566 return rc;
1567}
1568
1569int cifs_rename(struct inode *source_dir, struct dentry *source_dentry,
1570 struct inode *target_dir, struct dentry *target_dentry)
1571{
1572 char *fromName = NULL;
1573 char *toName = NULL;
1574 struct cifs_sb_info *cifs_sb;
1575 struct tcon_link *tlink;
1576 struct cifs_tcon *tcon;
1577 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
1578 FILE_UNIX_BASIC_INFO *info_buf_target;
1579 int xid, rc, tmprc;
1580
1581 cifs_sb = CIFS_SB(source_dir->i_sb);
1582 tlink = cifs_sb_tlink(cifs_sb);
1583 if (IS_ERR(tlink))
1584 return PTR_ERR(tlink);
1585 tcon = tlink_tcon(tlink);
1586
1587 xid = GetXid();
1588
1589 /*
1590 * we already have the rename sem so we do not need to
1591 * grab it again here to protect the path integrity
1592 */
1593 fromName = build_path_from_dentry(source_dentry);
1594 if (fromName == NULL) {
1595 rc = -ENOMEM;
1596 goto cifs_rename_exit;
1597 }
1598
1599 toName = build_path_from_dentry(target_dentry);
1600 if (toName == NULL) {
1601 rc = -ENOMEM;
1602 goto cifs_rename_exit;
1603 }
1604
1605 rc = cifs_do_rename(xid, source_dentry, fromName,
1606 target_dentry, toName);
1607
1608 if (rc == -EEXIST && tcon->unix_ext) {
1609 /*
1610 * Are src and dst hardlinks of same inode? We can
1611 * only tell with unix extensions enabled
1612 */
1613 info_buf_source =
1614 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
1615 GFP_KERNEL);
1616 if (info_buf_source == NULL) {
1617 rc = -ENOMEM;
1618 goto cifs_rename_exit;
1619 }
1620
1621 info_buf_target = info_buf_source + 1;
1622 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, fromName,
1623 info_buf_source,
1624 cifs_sb->local_nls,
1625 cifs_sb->mnt_cifs_flags &
1626 CIFS_MOUNT_MAP_SPECIAL_CHR);
1627 if (tmprc != 0)
1628 goto unlink_target;
1629
1630 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, toName,
1631 info_buf_target,
1632 cifs_sb->local_nls,
1633 cifs_sb->mnt_cifs_flags &
1634 CIFS_MOUNT_MAP_SPECIAL_CHR);
1635
1636 if (tmprc == 0 && (info_buf_source->UniqueId ==
1637 info_buf_target->UniqueId)) {
1638 /* same file, POSIX says that this is a noop */
1639 rc = 0;
1640 goto cifs_rename_exit;
1641 }
1642 } /* else ... BB we could add the same check for Windows by
1643 checking the UniqueId via FILE_INTERNAL_INFO */
1644
1645unlink_target:
1646 /* Try unlinking the target dentry if it's not negative */
1647 if (target_dentry->d_inode && (rc == -EACCES || rc == -EEXIST)) {
1648 tmprc = cifs_unlink(target_dir, target_dentry);
1649 if (tmprc)
1650 goto cifs_rename_exit;
1651
1652 rc = cifs_do_rename(xid, source_dentry, fromName,
1653 target_dentry, toName);
1654 }
1655
1656 /* force revalidate to go get info when needed */
1657 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
1658
1659 source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
1660 target_dir->i_mtime = current_fs_time(source_dir->i_sb);
1661
1662cifs_rename_exit:
1663 kfree(info_buf_source);
1664 kfree(fromName);
1665 kfree(toName);
1666 FreeXid(xid);
1667 cifs_put_tlink(tlink);
1668 return rc;
1669}
1670
1671static bool
1672cifs_inode_needs_reval(struct inode *inode)
1673{
1674 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
1675 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1676
1677 if (cifs_i->clientCanCacheRead)
1678 return false;
1679
1680 if (!lookupCacheEnabled)
1681 return true;
1682
1683 if (cifs_i->time == 0)
1684 return true;
1685
1686 if (!time_in_range(jiffies, cifs_i->time,
1687 cifs_i->time + cifs_sb->actimeo))
1688 return true;
1689
1690 /* hardlinked files w/ noserverino get "special" treatment */
1691 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
1692 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
1693 return true;
1694
1695 return false;
1696}
1697
1698/*
1699 * Zap the cache. Called when invalid_mapping flag is set.
1700 */
1701int
1702cifs_invalidate_mapping(struct inode *inode)
1703{
1704 int rc = 0;
1705 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
1706
1707 cifs_i->invalid_mapping = false;
1708
1709 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
1710 rc = invalidate_inode_pages2(inode->i_mapping);
1711 if (rc) {
1712 cERROR(1, "%s: could not invalidate inode %p", __func__,
1713 inode);
1714 cifs_i->invalid_mapping = true;
1715 }
1716 }
1717
1718 cifs_fscache_reset_inode_cookie(inode);
1719 return rc;
1720}
1721
1722int cifs_revalidate_file_attr(struct file *filp)
1723{
1724 int rc = 0;
1725 struct inode *inode = filp->f_path.dentry->d_inode;
1726 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
1727
1728 if (!cifs_inode_needs_reval(inode))
1729 return rc;
1730
1731 if (tlink_tcon(cfile->tlink)->unix_ext)
1732 rc = cifs_get_file_info_unix(filp);
1733 else
1734 rc = cifs_get_file_info(filp);
1735
1736 return rc;
1737}
1738
1739int cifs_revalidate_dentry_attr(struct dentry *dentry)
1740{
1741 int xid;
1742 int rc = 0;
1743 struct inode *inode = dentry->d_inode;
1744 struct super_block *sb = dentry->d_sb;
1745 char *full_path = NULL;
1746
1747 if (inode == NULL)
1748 return -ENOENT;
1749
1750 if (!cifs_inode_needs_reval(inode))
1751 return rc;
1752
1753 xid = GetXid();
1754
1755 /* can not safely grab the rename sem here if rename calls revalidate
1756 since that would deadlock */
1757 full_path = build_path_from_dentry(dentry);
1758 if (full_path == NULL) {
1759 rc = -ENOMEM;
1760 goto out;
1761 }
1762
1763 cFYI(1, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time "
1764 "%ld jiffies %ld", full_path, inode, inode->i_count.counter,
1765 dentry, dentry->d_time, jiffies);
1766
1767 if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
1768 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
1769 else
1770 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
1771 xid, NULL);
1772
1773out:
1774 kfree(full_path);
1775 FreeXid(xid);
1776 return rc;
1777}
1778
1779int cifs_revalidate_file(struct file *filp)
1780{
1781 int rc;
1782 struct inode *inode = filp->f_path.dentry->d_inode;
1783
1784 rc = cifs_revalidate_file_attr(filp);
1785 if (rc)
1786 return rc;
1787
1788 if (CIFS_I(inode)->invalid_mapping)
1789 rc = cifs_invalidate_mapping(inode);
1790 return rc;
1791}
1792
1793/* revalidate a dentry's inode attributes */
1794int cifs_revalidate_dentry(struct dentry *dentry)
1795{
1796 int rc;
1797 struct inode *inode = dentry->d_inode;
1798
1799 rc = cifs_revalidate_dentry_attr(dentry);
1800 if (rc)
1801 return rc;
1802
1803 if (CIFS_I(inode)->invalid_mapping)
1804 rc = cifs_invalidate_mapping(inode);
1805 return rc;
1806}
1807
1808int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1809 struct kstat *stat)
1810{
1811 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
1812 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1813 struct inode *inode = dentry->d_inode;
1814 int rc;
1815
1816 /*
1817 * We need to be sure that all dirty pages are written and the server
1818 * has actual ctime, mtime and file length.
1819 */
1820 if (!CIFS_I(inode)->clientCanCacheRead && inode->i_mapping &&
1821 inode->i_mapping->nrpages != 0) {
1822 rc = filemap_fdatawait(inode->i_mapping);
1823 if (rc) {
1824 mapping_set_error(inode->i_mapping, rc);
1825 return rc;
1826 }
1827 }
1828
1829 rc = cifs_revalidate_dentry_attr(dentry);
1830 if (rc)
1831 return rc;
1832
1833 generic_fillattr(inode, stat);
1834 stat->blksize = CIFS_MAX_MSGSIZE;
1835 stat->ino = CIFS_I(inode)->uniqueid;
1836
1837 /*
1838 * If on a multiuser mount without unix extensions, and the admin hasn't
1839 * overridden them, set the ownership to the fsuid/fsgid of the current
1840 * process.
1841 */
1842 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
1843 !tcon->unix_ext) {
1844 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
1845 stat->uid = current_fsuid();
1846 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
1847 stat->gid = current_fsgid();
1848 }
1849 return rc;
1850}
1851
1852static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1853{
1854 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1855 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1856 struct page *page;
1857 int rc = 0;
1858
1859 page = grab_cache_page(mapping, index);
1860 if (!page)
1861 return -ENOMEM;
1862
1863 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
1864 unlock_page(page);
1865 page_cache_release(page);
1866 return rc;
1867}
1868
1869static void cifs_setsize(struct inode *inode, loff_t offset)
1870{
1871 loff_t oldsize;
1872
1873 spin_lock(&inode->i_lock);
1874 oldsize = inode->i_size;
1875 i_size_write(inode, offset);
1876 spin_unlock(&inode->i_lock);
1877
1878 truncate_pagecache(inode, oldsize, offset);
1879}
1880
1881static int
1882cifs_set_file_size(struct inode *inode, struct iattr *attrs,
1883 int xid, char *full_path)
1884{
1885 int rc;
1886 struct cifsFileInfo *open_file;
1887 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1888 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1889 struct tcon_link *tlink = NULL;
1890 struct cifs_tcon *pTcon = NULL;
1891 struct cifs_io_parms io_parms;
1892
1893 /*
1894 * To avoid spurious oplock breaks from server, in the case of
1895 * inodes that we already have open, avoid doing path based
1896 * setting of file size if we can do it by handle.
1897 * This keeps our caching token (oplock) and avoids timeouts
1898 * when the local oplock break takes longer to flush
1899 * writebehind data than the SMB timeout for the SetPathInfo
1900 * request would allow
1901 */
1902 open_file = find_writable_file(cifsInode, true);
1903 if (open_file) {
1904 __u16 nfid = open_file->netfid;
1905 __u32 npid = open_file->pid;
1906 pTcon = tlink_tcon(open_file->tlink);
1907 rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size, nfid,
1908 npid, false);
1909 cifsFileInfo_put(open_file);
1910 cFYI(1, "SetFSize for attrs rc = %d", rc);
1911 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1912 unsigned int bytes_written;
1913
1914 io_parms.netfid = nfid;
1915 io_parms.pid = npid;
1916 io_parms.tcon = pTcon;
1917 io_parms.offset = 0;
1918 io_parms.length = attrs->ia_size;
1919 rc = CIFSSMBWrite(xid, &io_parms, &bytes_written,
1920 NULL, NULL, 1);
1921 cFYI(1, "Wrt seteof rc %d", rc);
1922 }
1923 } else
1924 rc = -EINVAL;
1925
1926 if (rc != 0) {
1927 if (pTcon == NULL) {
1928 tlink = cifs_sb_tlink(cifs_sb);
1929 if (IS_ERR(tlink))
1930 return PTR_ERR(tlink);
1931 pTcon = tlink_tcon(tlink);
1932 }
1933
1934 /* Set file size by pathname rather than by handle
1935 either because no valid, writeable file handle for
1936 it was found or because there was an error setting
1937 it by handle */
1938 rc = CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size,
1939 false, cifs_sb->local_nls,
1940 cifs_sb->mnt_cifs_flags &
1941 CIFS_MOUNT_MAP_SPECIAL_CHR);
1942 cFYI(1, "SetEOF by path (setattrs) rc = %d", rc);
1943 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1944 __u16 netfid;
1945 int oplock = 0;
1946
1947 rc = SMBLegacyOpen(xid, pTcon, full_path,
1948 FILE_OPEN, GENERIC_WRITE,
1949 CREATE_NOT_DIR, &netfid, &oplock, NULL,
1950 cifs_sb->local_nls,
1951 cifs_sb->mnt_cifs_flags &
1952 CIFS_MOUNT_MAP_SPECIAL_CHR);
1953 if (rc == 0) {
1954 unsigned int bytes_written;
1955
1956 io_parms.netfid = netfid;
1957 io_parms.pid = current->tgid;
1958 io_parms.tcon = pTcon;
1959 io_parms.offset = 0;
1960 io_parms.length = attrs->ia_size;
1961 rc = CIFSSMBWrite(xid, &io_parms,
1962 &bytes_written,
1963 NULL, NULL, 1);
1964 cFYI(1, "wrt seteof rc %d", rc);
1965 CIFSSMBClose(xid, pTcon, netfid);
1966 }
1967 }
1968 if (tlink)
1969 cifs_put_tlink(tlink);
1970 }
1971
1972 if (rc == 0) {
1973 cifsInode->server_eof = attrs->ia_size;
1974 cifs_setsize(inode, attrs->ia_size);
1975 cifs_truncate_page(inode->i_mapping, inode->i_size);
1976 }
1977
1978 return rc;
1979}
1980
1981static int
1982cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
1983{
1984 int rc;
1985 int xid;
1986 char *full_path = NULL;
1987 struct inode *inode = direntry->d_inode;
1988 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1989 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1990 struct tcon_link *tlink;
1991 struct cifs_tcon *pTcon;
1992 struct cifs_unix_set_info_args *args = NULL;
1993 struct cifsFileInfo *open_file;
1994
1995 cFYI(1, "setattr_unix on file %s attrs->ia_valid=0x%x",
1996 direntry->d_name.name, attrs->ia_valid);
1997
1998 xid = GetXid();
1999
2000 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2001 attrs->ia_valid |= ATTR_FORCE;
2002
2003 rc = inode_change_ok(inode, attrs);
2004 if (rc < 0)
2005 goto out;
2006
2007 full_path = build_path_from_dentry(direntry);
2008 if (full_path == NULL) {
2009 rc = -ENOMEM;
2010 goto out;
2011 }
2012
2013 /*
2014 * Attempt to flush data before changing attributes. We need to do
2015 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2016 * ownership or mode then we may also need to do this. Here, we take
2017 * the safe way out and just do the flush on all setattr requests. If
2018 * the flush returns error, store it to report later and continue.
2019 *
2020 * BB: This should be smarter. Why bother flushing pages that
2021 * will be truncated anyway? Also, should we error out here if
2022 * the flush returns error?
2023 */
2024 rc = filemap_write_and_wait(inode->i_mapping);
2025 mapping_set_error(inode->i_mapping, rc);
2026 rc = 0;
2027
2028 if (attrs->ia_valid & ATTR_SIZE) {
2029 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2030 if (rc != 0)
2031 goto out;
2032 }
2033
2034 /* skip mode change if it's just for clearing setuid/setgid */
2035 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2036 attrs->ia_valid &= ~ATTR_MODE;
2037
2038 args = kmalloc(sizeof(*args), GFP_KERNEL);
2039 if (args == NULL) {
2040 rc = -ENOMEM;
2041 goto out;
2042 }
2043
2044 /* set up the struct */
2045 if (attrs->ia_valid & ATTR_MODE)
2046 args->mode = attrs->ia_mode;
2047 else
2048 args->mode = NO_CHANGE_64;
2049
2050 if (attrs->ia_valid & ATTR_UID)
2051 args->uid = attrs->ia_uid;
2052 else
2053 args->uid = NO_CHANGE_64;
2054
2055 if (attrs->ia_valid & ATTR_GID)
2056 args->gid = attrs->ia_gid;
2057 else
2058 args->gid = NO_CHANGE_64;
2059
2060 if (attrs->ia_valid & ATTR_ATIME)
2061 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2062 else
2063 args->atime = NO_CHANGE_64;
2064
2065 if (attrs->ia_valid & ATTR_MTIME)
2066 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2067 else
2068 args->mtime = NO_CHANGE_64;
2069
2070 if (attrs->ia_valid & ATTR_CTIME)
2071 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2072 else
2073 args->ctime = NO_CHANGE_64;
2074
2075 args->device = 0;
2076 open_file = find_writable_file(cifsInode, true);
2077 if (open_file) {
2078 u16 nfid = open_file->netfid;
2079 u32 npid = open_file->pid;
2080 pTcon = tlink_tcon(open_file->tlink);
2081 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2082 cifsFileInfo_put(open_file);
2083 } else {
2084 tlink = cifs_sb_tlink(cifs_sb);
2085 if (IS_ERR(tlink)) {
2086 rc = PTR_ERR(tlink);
2087 goto out;
2088 }
2089 pTcon = tlink_tcon(tlink);
2090 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2091 cifs_sb->local_nls,
2092 cifs_sb->mnt_cifs_flags &
2093 CIFS_MOUNT_MAP_SPECIAL_CHR);
2094 cifs_put_tlink(tlink);
2095 }
2096
2097 if (rc)
2098 goto out;
2099
2100 if ((attrs->ia_valid & ATTR_SIZE) &&
2101 attrs->ia_size != i_size_read(inode))
2102 truncate_setsize(inode, attrs->ia_size);
2103
2104 setattr_copy(inode, attrs);
2105 mark_inode_dirty(inode);
2106
2107 /* force revalidate when any of these times are set since some
2108 of the fs types (eg ext3, fat) do not have fine enough
2109 time granularity to match protocol, and we do not have a
2110 a way (yet) to query the server fs's time granularity (and
2111 whether it rounds times down).
2112 */
2113 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2114 cifsInode->time = 0;
2115out:
2116 kfree(args);
2117 kfree(full_path);
2118 FreeXid(xid);
2119 return rc;
2120}
2121
2122static int
2123cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2124{
2125 int xid;
2126 uid_t uid = NO_CHANGE_32;
2127 gid_t gid = NO_CHANGE_32;
2128 struct inode *inode = direntry->d_inode;
2129 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2130 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2131 char *full_path = NULL;
2132 int rc = -EACCES;
2133 __u32 dosattr = 0;
2134 __u64 mode = NO_CHANGE_64;
2135
2136 xid = GetXid();
2137
2138 cFYI(1, "setattr on file %s attrs->iavalid 0x%x",
2139 direntry->d_name.name, attrs->ia_valid);
2140
2141 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2142 attrs->ia_valid |= ATTR_FORCE;
2143
2144 rc = inode_change_ok(inode, attrs);
2145 if (rc < 0) {
2146 FreeXid(xid);
2147 return rc;
2148 }
2149
2150 full_path = build_path_from_dentry(direntry);
2151 if (full_path == NULL) {
2152 rc = -ENOMEM;
2153 FreeXid(xid);
2154 return rc;
2155 }
2156
2157 /*
2158 * Attempt to flush data before changing attributes. We need to do
2159 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2160 * ownership or mode then we may also need to do this. Here, we take
2161 * the safe way out and just do the flush on all setattr requests. If
2162 * the flush returns error, store it to report later and continue.
2163 *
2164 * BB: This should be smarter. Why bother flushing pages that
2165 * will be truncated anyway? Also, should we error out here if
2166 * the flush returns error?
2167 */
2168 rc = filemap_write_and_wait(inode->i_mapping);
2169 mapping_set_error(inode->i_mapping, rc);
2170 rc = 0;
2171
2172 if (attrs->ia_valid & ATTR_SIZE) {
2173 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2174 if (rc != 0)
2175 goto cifs_setattr_exit;
2176 }
2177
2178 if (attrs->ia_valid & ATTR_UID)
2179 uid = attrs->ia_uid;
2180
2181 if (attrs->ia_valid & ATTR_GID)
2182 gid = attrs->ia_gid;
2183
2184#ifdef CONFIG_CIFS_ACL
2185 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
2186 if (uid != NO_CHANGE_32 || gid != NO_CHANGE_32) {
2187 rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
2188 uid, gid);
2189 if (rc) {
2190 cFYI(1, "%s: Setting id failed with error: %d",
2191 __func__, rc);
2192 goto cifs_setattr_exit;
2193 }
2194 }
2195 } else
2196#endif /* CONFIG_CIFS_ACL */
2197 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
2198 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
2199
2200 /* skip mode change if it's just for clearing setuid/setgid */
2201 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2202 attrs->ia_valid &= ~ATTR_MODE;
2203
2204 if (attrs->ia_valid & ATTR_MODE) {
2205 mode = attrs->ia_mode;
2206 rc = 0;
2207#ifdef CONFIG_CIFS_ACL
2208 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
2209 rc = id_mode_to_cifs_acl(inode, full_path, mode,
2210 NO_CHANGE_32, NO_CHANGE_32);
2211 if (rc) {
2212 cFYI(1, "%s: Setting ACL failed with error: %d",
2213 __func__, rc);
2214 goto cifs_setattr_exit;
2215 }
2216 } else
2217#endif /* CONFIG_CIFS_ACL */
2218 if (((mode & S_IWUGO) == 0) &&
2219 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
2220
2221 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2222
2223 /* fix up mode if we're not using dynperm */
2224 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2225 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2226 } else if ((mode & S_IWUGO) &&
2227 (cifsInode->cifsAttrs & ATTR_READONLY)) {
2228
2229 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2230 /* Attributes of 0 are ignored */
2231 if (dosattr == 0)
2232 dosattr |= ATTR_NORMAL;
2233
2234 /* reset local inode permissions to normal */
2235 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2236 attrs->ia_mode &= ~(S_IALLUGO);
2237 if (S_ISDIR(inode->i_mode))
2238 attrs->ia_mode |=
2239 cifs_sb->mnt_dir_mode;
2240 else
2241 attrs->ia_mode |=
2242 cifs_sb->mnt_file_mode;
2243 }
2244 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2245 /* ignore mode change - ATTR_READONLY hasn't changed */
2246 attrs->ia_valid &= ~ATTR_MODE;
2247 }
2248 }
2249
2250 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
2251 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
2252 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2253 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
2254
2255 /* Even if error on time set, no sense failing the call if
2256 the server would set the time to a reasonable value anyway,
2257 and this check ensures that we are not being called from
2258 sys_utimes in which case we ought to fail the call back to
2259 the user when the server rejects the call */
2260 if ((rc) && (attrs->ia_valid &
2261 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
2262 rc = 0;
2263 }
2264
2265 /* do not need local check to inode_check_ok since the server does
2266 that */
2267 if (rc)
2268 goto cifs_setattr_exit;
2269
2270 if ((attrs->ia_valid & ATTR_SIZE) &&
2271 attrs->ia_size != i_size_read(inode))
2272 truncate_setsize(inode, attrs->ia_size);
2273
2274 setattr_copy(inode, attrs);
2275 mark_inode_dirty(inode);
2276
2277cifs_setattr_exit:
2278 kfree(full_path);
2279 FreeXid(xid);
2280 return rc;
2281}
2282
2283int
2284cifs_setattr(struct dentry *direntry, struct iattr *attrs)
2285{
2286 struct inode *inode = direntry->d_inode;
2287 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2288 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
2289
2290 if (pTcon->unix_ext)
2291 return cifs_setattr_unix(direntry, attrs);
2292
2293 return cifs_setattr_nounix(direntry, attrs);
2294
2295 /* BB: add cifs_setattr_legacy for really old servers */
2296}
2297
2298#if 0
2299void cifs_delete_inode(struct inode *inode)
2300{
2301 cFYI(1, "In cifs_delete_inode, inode = 0x%p", inode);
2302 /* may have to add back in if and when safe distributed caching of
2303 directories added e.g. via FindNotify */
2304}
2305#endif