1 /*
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 <linux/freezer.h>
26 #include <linux/sched/signal.h>
27 #include <linux/wait_bit.h>
28 #include <linux/fiemap.h>
29
30 #include <asm/div64.h>
31 #include "cifsfs.h"
32 #include "cifspdu.h"
33 #include "cifsglob.h"
34 #include "cifsproto.h"
35 #include "smb2proto.h"
36 #include "cifs_debug.h"
37 #include "cifs_fs_sb.h"
38 #include "cifs_unicode.h"
39 #include "fscache.h"
40
41
cifs_set_ops(struct inode * inode)42 static void cifs_set_ops(struct inode *inode)
43 {
44 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
45
46 switch (inode->i_mode & S_IFMT) {
47 case S_IFREG:
48 inode->i_op = &cifs_file_inode_ops;
49 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
50 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
51 inode->i_fop = &cifs_file_direct_nobrl_ops;
52 else
53 inode->i_fop = &cifs_file_direct_ops;
54 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
55 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
56 inode->i_fop = &cifs_file_strict_nobrl_ops;
57 else
58 inode->i_fop = &cifs_file_strict_ops;
59 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
60 inode->i_fop = &cifs_file_nobrl_ops;
61 else { /* not direct, send byte range locks */
62 inode->i_fop = &cifs_file_ops;
63 }
64
65 /* check if server can support readpages */
66 if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
67 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
68 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
69 else
70 inode->i_data.a_ops = &cifs_addr_ops;
71 break;
72 case S_IFDIR:
73 #ifdef CONFIG_CIFS_DFS_UPCALL
74 if (IS_AUTOMOUNT(inode)) {
75 inode->i_op = &cifs_dfs_referral_inode_operations;
76 } else {
77 #else /* NO DFS support, treat as a directory */
78 {
79 #endif
80 inode->i_op = &cifs_dir_inode_ops;
81 inode->i_fop = &cifs_dir_ops;
82 }
83 break;
84 case S_IFLNK:
85 inode->i_op = &cifs_symlink_inode_ops;
86 break;
87 default:
88 init_special_inode(inode, inode->i_mode, inode->i_rdev);
89 break;
90 }
91 }
92
93 /* check inode attributes against fattr. If they don't match, tag the
94 * inode for cache invalidation
95 */
96 static void
97 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
98 {
99 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
100
101 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
102 __func__, cifs_i->uniqueid);
103
104 if (inode->i_state & I_NEW) {
105 cifs_dbg(FYI, "%s: inode %llu is new\n",
106 __func__, cifs_i->uniqueid);
107 return;
108 }
109
110 /* don't bother with revalidation if we have an oplock */
111 if (CIFS_CACHE_READ(cifs_i)) {
112 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
113 __func__, cifs_i->uniqueid);
114 return;
115 }
116
117 /* revalidate if mtime or size have changed */
118 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
119 if (timespec64_equal(&inode->i_mtime, &fattr->cf_mtime) &&
120 cifs_i->server_eof == fattr->cf_eof) {
121 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
122 __func__, cifs_i->uniqueid);
123 return;
124 }
125
126 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
127 __func__, cifs_i->uniqueid);
128 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
129 }
130
131 /*
132 * copy nlink to the inode, unless it wasn't provided. Provide
133 * sane values if we don't have an existing one and none was provided
134 */
135 static void
136 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
137 {
138 /*
139 * if we're in a situation where we can't trust what we
140 * got from the server (readdir, some non-unix cases)
141 * fake reasonable values
142 */
143 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
144 /* only provide fake values on a new inode */
145 if (inode->i_state & I_NEW) {
146 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
147 set_nlink(inode, 2);
148 else
149 set_nlink(inode, 1);
150 }
151 return;
152 }
153
154 /* we trust the server, so update it */
155 set_nlink(inode, fattr->cf_nlink);
156 }
157
158 /* populate an inode with info from a cifs_fattr struct */
159 void
160 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
161 {
162 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
163 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
164
165 cifs_revalidate_cache(inode, fattr);
166
167 spin_lock(&inode->i_lock);
168 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
169 fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
170 fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
171 /* we do not want atime to be less than mtime, it broke some apps */
172 if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
173 inode->i_atime = fattr->cf_mtime;
174 else
175 inode->i_atime = fattr->cf_atime;
176 inode->i_mtime = fattr->cf_mtime;
177 inode->i_ctime = fattr->cf_ctime;
178 inode->i_rdev = fattr->cf_rdev;
179 cifs_nlink_fattr_to_inode(inode, fattr);
180 inode->i_uid = fattr->cf_uid;
181 inode->i_gid = fattr->cf_gid;
182
183 /* if dynperm is set, don't clobber existing mode */
184 if (inode->i_state & I_NEW ||
185 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
186 inode->i_mode = fattr->cf_mode;
187
188 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
189
190 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
191 cifs_i->time = 0;
192 else
193 cifs_i->time = jiffies;
194
195 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
196 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
197 else
198 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
199
200 cifs_i->server_eof = fattr->cf_eof;
201 /*
202 * Can't safely change the file size here if the client is writing to
203 * it due to potential races.
204 */
205 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
206 i_size_write(inode, fattr->cf_eof);
207
208 /*
209 * i_blocks is not related to (i_size / i_blksize),
210 * but instead 512 byte (2**9) size is required for
211 * calculating num blocks.
212 */
213 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
214 }
215 spin_unlock(&inode->i_lock);
216
217 if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
218 inode->i_flags |= S_AUTOMOUNT;
219 if (inode->i_state & I_NEW)
220 cifs_set_ops(inode);
221 }
222
223 void
224 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
225 {
226 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
227
228 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
229 return;
230
231 fattr->cf_uniqueid = iunique(sb, ROOT_I);
232 }
233
234 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
235 void
236 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
237 struct cifs_sb_info *cifs_sb)
238 {
239 memset(fattr, 0, sizeof(*fattr));
240 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
241 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
242 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
243
244 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
245 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
246 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
247 /* old POSIX extensions don't get create time */
248
249 fattr->cf_mode = le64_to_cpu(info->Permissions);
250
251 /*
252 * Since we set the inode type below we need to mask off
253 * to avoid strange results if bits set above.
254 */
255 fattr->cf_mode &= ~S_IFMT;
256 switch (le32_to_cpu(info->Type)) {
257 case UNIX_FILE:
258 fattr->cf_mode |= S_IFREG;
259 fattr->cf_dtype = DT_REG;
260 break;
261 case UNIX_SYMLINK:
262 fattr->cf_mode |= S_IFLNK;
263 fattr->cf_dtype = DT_LNK;
264 break;
265 case UNIX_DIR:
266 fattr->cf_mode |= S_IFDIR;
267 fattr->cf_dtype = DT_DIR;
268 break;
269 case UNIX_CHARDEV:
270 fattr->cf_mode |= S_IFCHR;
271 fattr->cf_dtype = DT_CHR;
272 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
273 le64_to_cpu(info->DevMinor) & MINORMASK);
274 break;
275 case UNIX_BLOCKDEV:
276 fattr->cf_mode |= S_IFBLK;
277 fattr->cf_dtype = DT_BLK;
278 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
279 le64_to_cpu(info->DevMinor) & MINORMASK);
280 break;
281 case UNIX_FIFO:
282 fattr->cf_mode |= S_IFIFO;
283 fattr->cf_dtype = DT_FIFO;
284 break;
285 case UNIX_SOCKET:
286 fattr->cf_mode |= S_IFSOCK;
287 fattr->cf_dtype = DT_SOCK;
288 break;
289 default:
290 /* safest to call it a file if we do not know */
291 fattr->cf_mode |= S_IFREG;
292 fattr->cf_dtype = DT_REG;
293 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
294 break;
295 }
296
297 fattr->cf_uid = cifs_sb->mnt_uid;
298 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
299 u64 id = le64_to_cpu(info->Uid);
300 if (id < ((uid_t)-1)) {
301 kuid_t uid = make_kuid(&init_user_ns, id);
302 if (uid_valid(uid))
303 fattr->cf_uid = uid;
304 }
305 }
306
307 fattr->cf_gid = cifs_sb->mnt_gid;
308 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
309 u64 id = le64_to_cpu(info->Gid);
310 if (id < ((gid_t)-1)) {
311 kgid_t gid = make_kgid(&init_user_ns, id);
312 if (gid_valid(gid))
313 fattr->cf_gid = gid;
314 }
315 }
316
317 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
318 }
319
320 /*
321 * Fill a cifs_fattr struct with fake inode info.
322 *
323 * Needed to setup cifs_fattr data for the directory which is the
324 * junction to the new submount (ie to setup the fake directory
325 * which represents a DFS referral).
326 */
327 static void
328 cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
329 {
330 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
331
332 cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
333
334 memset(fattr, 0, sizeof(*fattr));
335 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
336 fattr->cf_uid = cifs_sb->mnt_uid;
337 fattr->cf_gid = cifs_sb->mnt_gid;
338 ktime_get_coarse_real_ts64(&fattr->cf_mtime);
339 fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
340 fattr->cf_nlink = 2;
341 fattr->cf_flags = CIFS_FATTR_DFS_REFERRAL;
342 }
343
344 static int
345 cifs_get_file_info_unix(struct file *filp)
346 {
347 int rc;
348 unsigned int xid;
349 FILE_UNIX_BASIC_INFO find_data;
350 struct cifs_fattr fattr;
351 struct inode *inode = file_inode(filp);
352 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
353 struct cifsFileInfo *cfile = filp->private_data;
354 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
355
356 xid = get_xid();
357 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
358 if (!rc) {
359 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
360 } else if (rc == -EREMOTE) {
361 cifs_create_dfs_fattr(&fattr, inode->i_sb);
362 rc = 0;
363 } else
364 goto cifs_gfiunix_out;
365
366 cifs_fattr_to_inode(inode, &fattr);
367
368 cifs_gfiunix_out:
369 free_xid(xid);
370 return rc;
371 }
372
373 int cifs_get_inode_info_unix(struct inode **pinode,
374 const unsigned char *full_path,
375 struct super_block *sb, unsigned int xid)
376 {
377 int rc;
378 FILE_UNIX_BASIC_INFO find_data;
379 struct cifs_fattr fattr;
380 struct cifs_tcon *tcon;
381 struct tcon_link *tlink;
382 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
383
384 cifs_dbg(FYI, "Getting info on %s\n", full_path);
385
386 tlink = cifs_sb_tlink(cifs_sb);
387 if (IS_ERR(tlink))
388 return PTR_ERR(tlink);
389 tcon = tlink_tcon(tlink);
390
391 /* could have done a find first instead but this returns more info */
392 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
393 cifs_sb->local_nls, cifs_remap(cifs_sb));
394 cifs_put_tlink(tlink);
395
396 if (!rc) {
397 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
398 } else if (rc == -EREMOTE) {
399 cifs_create_dfs_fattr(&fattr, sb);
400 rc = 0;
401 } else {
402 return rc;
403 }
404
405 /* check for Minshall+French symlinks */
406 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
407 int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
408 full_path);
409 if (tmprc)
410 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
411 }
412
413 if (*pinode == NULL) {
414 /* get new inode */
415 cifs_fill_uniqueid(sb, &fattr);
416 *pinode = cifs_iget(sb, &fattr);
417 if (!*pinode)
418 rc = -ENOMEM;
419 } else {
420 /* we already have inode, update it */
421
422 /* if uniqueid is different, return error */
423 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
424 CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
425 CIFS_I(*pinode)->time = 0; /* force reval */
426 rc = -ESTALE;
427 goto cgiiu_exit;
428 }
429
430 /* if filetype is different, return error */
431 if (unlikely(inode_wrong_type(*pinode, fattr.cf_mode))) {
432 CIFS_I(*pinode)->time = 0; /* force reval */
433 rc = -ESTALE;
434 goto cgiiu_exit;
435 }
436
437 cifs_fattr_to_inode(*pinode, &fattr);
438 }
439
440 cgiiu_exit:
441 return rc;
442 }
443
444 static int
445 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
446 struct cifs_sb_info *cifs_sb, unsigned int xid)
447 {
448 int rc;
449 __u32 oplock;
450 struct tcon_link *tlink;
451 struct cifs_tcon *tcon;
452 struct cifs_fid fid;
453 struct cifs_open_parms oparms;
454 struct cifs_io_parms io_parms = {0};
455 char buf[24];
456 unsigned int bytes_read;
457 char *pbuf;
458 int buf_type = CIFS_NO_BUFFER;
459
460 pbuf = buf;
461
462 fattr->cf_mode &= ~S_IFMT;
463
464 if (fattr->cf_eof == 0) {
465 fattr->cf_mode |= S_IFIFO;
466 fattr->cf_dtype = DT_FIFO;
467 return 0;
468 } else if (fattr->cf_eof < 8) {
469 fattr->cf_mode |= S_IFREG;
470 fattr->cf_dtype = DT_REG;
471 return -EINVAL; /* EOPNOTSUPP? */
472 }
473
474 tlink = cifs_sb_tlink(cifs_sb);
475 if (IS_ERR(tlink))
476 return PTR_ERR(tlink);
477 tcon = tlink_tcon(tlink);
478
479 oparms.tcon = tcon;
480 oparms.cifs_sb = cifs_sb;
481 oparms.desired_access = GENERIC_READ;
482 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
483 oparms.disposition = FILE_OPEN;
484 oparms.path = path;
485 oparms.fid = &fid;
486 oparms.reconnect = false;
487
488 if (tcon->ses->server->oplocks)
489 oplock = REQ_OPLOCK;
490 else
491 oplock = 0;
492 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
493 if (rc) {
494 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
495 cifs_put_tlink(tlink);
496 return rc;
497 }
498
499 /* Read header */
500 io_parms.netfid = fid.netfid;
501 io_parms.pid = current->tgid;
502 io_parms.tcon = tcon;
503 io_parms.offset = 0;
504 io_parms.length = 24;
505
506 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
507 &bytes_read, &pbuf, &buf_type);
508 if ((rc == 0) && (bytes_read >= 8)) {
509 if (memcmp("IntxBLK", pbuf, 8) == 0) {
510 cifs_dbg(FYI, "Block device\n");
511 fattr->cf_mode |= S_IFBLK;
512 fattr->cf_dtype = DT_BLK;
513 if (bytes_read == 24) {
514 /* we have enough to decode dev num */
515 __u64 mjr; /* major */
516 __u64 mnr; /* minor */
517 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
518 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
519 fattr->cf_rdev = MKDEV(mjr, mnr);
520 }
521 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
522 cifs_dbg(FYI, "Char device\n");
523 fattr->cf_mode |= S_IFCHR;
524 fattr->cf_dtype = DT_CHR;
525 if (bytes_read == 24) {
526 /* we have enough to decode dev num */
527 __u64 mjr; /* major */
528 __u64 mnr; /* minor */
529 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
530 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
531 fattr->cf_rdev = MKDEV(mjr, mnr);
532 }
533 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
534 cifs_dbg(FYI, "Symlink\n");
535 fattr->cf_mode |= S_IFLNK;
536 fattr->cf_dtype = DT_LNK;
537 } else {
538 fattr->cf_mode |= S_IFREG; /* file? */
539 fattr->cf_dtype = DT_REG;
540 rc = -EOPNOTSUPP;
541 }
542 } else {
543 fattr->cf_mode |= S_IFREG; /* then it is a file */
544 fattr->cf_dtype = DT_REG;
545 rc = -EOPNOTSUPP; /* or some unknown SFU type */
546 }
547
548 tcon->ses->server->ops->close(xid, tcon, &fid);
549 cifs_put_tlink(tlink);
550 return rc;
551 }
552
553 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
554
555 /*
556 * Fetch mode bits as provided by SFU.
557 *
558 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
559 */
560 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
561 struct cifs_sb_info *cifs_sb, unsigned int xid)
562 {
563 #ifdef CONFIG_CIFS_XATTR
564 ssize_t rc;
565 char ea_value[4];
566 __u32 mode;
567 struct tcon_link *tlink;
568 struct cifs_tcon *tcon;
569
570 tlink = cifs_sb_tlink(cifs_sb);
571 if (IS_ERR(tlink))
572 return PTR_ERR(tlink);
573 tcon = tlink_tcon(tlink);
574
575 if (tcon->ses->server->ops->query_all_EAs == NULL) {
576 cifs_put_tlink(tlink);
577 return -EOPNOTSUPP;
578 }
579
580 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
581 "SETFILEBITS", ea_value, 4 /* size of buf */,
582 cifs_sb);
583 cifs_put_tlink(tlink);
584 if (rc < 0)
585 return (int)rc;
586 else if (rc > 3) {
587 mode = le32_to_cpu(*((__le32 *)ea_value));
588 fattr->cf_mode &= ~SFBITS_MASK;
589 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
590 mode, fattr->cf_mode);
591 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
592 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
593 }
594
595 return 0;
596 #else
597 return -EOPNOTSUPP;
598 #endif
599 }
600
601 /* Fill a cifs_fattr struct with info from POSIX info struct */
602 static void
603 smb311_posix_info_to_fattr(struct cifs_fattr *fattr, struct smb311_posix_qinfo *info,
604 struct super_block *sb, bool adjust_tz, bool symlink)
605 {
606 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
607 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
608
609 memset(fattr, 0, sizeof(*fattr));
610
611 /* no fattr->flags to set */
612 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
613 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
614
615 if (info->LastAccessTime)
616 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
617 else
618 ktime_get_coarse_real_ts64(&fattr->cf_atime);
619
620 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
621 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
622
623 if (adjust_tz) {
624 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
625 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
626 }
627
628 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
629 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
630 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
631
632 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
633 fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
634 /* The srv fs device id is overridden on network mount so setting rdev isn't needed here */
635 /* fattr->cf_rdev = le32_to_cpu(info->DeviceId); */
636
637 if (symlink) {
638 fattr->cf_mode |= S_IFLNK;
639 fattr->cf_dtype = DT_LNK;
640 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
641 fattr->cf_mode |= S_IFDIR;
642 fattr->cf_dtype = DT_DIR;
643 } else { /* file */
644 fattr->cf_mode |= S_IFREG;
645 fattr->cf_dtype = DT_REG;
646 }
647 /* else if reparse point ... TODO: add support for FIFO and blk dev; special file types */
648
649 fattr->cf_uid = cifs_sb->mnt_uid; /* TODO: map uid and gid from SID */
650 fattr->cf_gid = cifs_sb->mnt_gid;
651
652 cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
653 fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
654 }
655
656
657 /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
658 static void
659 cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
660 struct super_block *sb, bool adjust_tz,
661 bool symlink, u32 reparse_tag)
662 {
663 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
664 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
665
666 memset(fattr, 0, sizeof(*fattr));
667 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
668 if (info->DeletePending)
669 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
670
671 if (info->LastAccessTime)
672 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
673 else
674 ktime_get_coarse_real_ts64(&fattr->cf_atime);
675
676 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
677 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
678
679 if (adjust_tz) {
680 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
681 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
682 }
683
684 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
685 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
686 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
687
688 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
689 if (reparse_tag == IO_REPARSE_TAG_LX_SYMLINK) {
690 fattr->cf_mode |= S_IFLNK | cifs_sb->mnt_file_mode;
691 fattr->cf_dtype = DT_LNK;
692 } else if (reparse_tag == IO_REPARSE_TAG_LX_FIFO) {
693 fattr->cf_mode |= S_IFIFO | cifs_sb->mnt_file_mode;
694 fattr->cf_dtype = DT_FIFO;
695 } else if (reparse_tag == IO_REPARSE_TAG_AF_UNIX) {
696 fattr->cf_mode |= S_IFSOCK | cifs_sb->mnt_file_mode;
697 fattr->cf_dtype = DT_SOCK;
698 } else if (reparse_tag == IO_REPARSE_TAG_LX_CHR) {
699 fattr->cf_mode |= S_IFCHR | cifs_sb->mnt_file_mode;
700 fattr->cf_dtype = DT_CHR;
701 } else if (reparse_tag == IO_REPARSE_TAG_LX_BLK) {
702 fattr->cf_mode |= S_IFBLK | cifs_sb->mnt_file_mode;
703 fattr->cf_dtype = DT_BLK;
704 } else if (symlink) { /* TODO add more reparse tag checks */
705 fattr->cf_mode = S_IFLNK;
706 fattr->cf_dtype = DT_LNK;
707 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
708 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
709 fattr->cf_dtype = DT_DIR;
710 /*
711 * Server can return wrong NumberOfLinks value for directories
712 * when Unix extensions are disabled - fake it.
713 */
714 if (!tcon->unix_ext)
715 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
716 } else {
717 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
718 fattr->cf_dtype = DT_REG;
719
720 /* clear write bits if ATTR_READONLY is set */
721 if (fattr->cf_cifsattrs & ATTR_READONLY)
722 fattr->cf_mode &= ~(S_IWUGO);
723
724 /*
725 * Don't accept zero nlink from non-unix servers unless
726 * delete is pending. Instead mark it as unknown.
727 */
728 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
729 !info->DeletePending) {
730 cifs_dbg(VFS, "bogus file nlink value %u\n",
731 fattr->cf_nlink);
732 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
733 }
734 }
735
736 fattr->cf_uid = cifs_sb->mnt_uid;
737 fattr->cf_gid = cifs_sb->mnt_gid;
738 }
739
740 static int
741 cifs_get_file_info(struct file *filp)
742 {
743 int rc;
744 unsigned int xid;
745 FILE_ALL_INFO find_data;
746 struct cifs_fattr fattr;
747 struct inode *inode = file_inode(filp);
748 struct cifsFileInfo *cfile = filp->private_data;
749 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
750 struct TCP_Server_Info *server = tcon->ses->server;
751
752 if (!server->ops->query_file_info)
753 return -ENOSYS;
754
755 xid = get_xid();
756 rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
757 switch (rc) {
758 case 0:
759 /* TODO: add support to query reparse tag */
760 cifs_all_info_to_fattr(&fattr, &find_data, inode->i_sb, false,
761 false, 0 /* no reparse tag */);
762 break;
763 case -EREMOTE:
764 cifs_create_dfs_fattr(&fattr, inode->i_sb);
765 rc = 0;
766 break;
767 case -EOPNOTSUPP:
768 case -EINVAL:
769 /*
770 * FIXME: legacy server -- fall back to path-based call?
771 * for now, just skip revalidating and mark inode for
772 * immediate reval.
773 */
774 rc = 0;
775 CIFS_I(inode)->time = 0;
776 default:
777 goto cgfi_exit;
778 }
779
780 /*
781 * don't bother with SFU junk here -- just mark inode as needing
782 * revalidation.
783 */
784 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
785 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
786 cifs_fattr_to_inode(inode, &fattr);
787 cgfi_exit:
788 free_xid(xid);
789 return rc;
790 }
791
792 /* Simple function to return a 64 bit hash of string. Rarely called */
793 static __u64 simple_hashstr(const char *str)
794 {
795 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
796 __u64 hash = 0;
797
798 while (*str)
799 hash = (hash + (__u64) *str++) * hash_mult;
800
801 return hash;
802 }
803
804 /**
805 * cifs_backup_query_path_info - SMB1 fallback code to get ino
806 *
807 * Fallback code to get file metadata when we don't have access to
808 * @full_path (EACCES) and have backup creds.
809 *
810 * @data will be set to search info result buffer
811 * @resp_buf will be set to cifs resp buf and needs to be freed with
812 * cifs_buf_release() when done with @data.
813 */
814 static int
815 cifs_backup_query_path_info(int xid,
816 struct cifs_tcon *tcon,
817 struct super_block *sb,
818 const char *full_path,
819 void **resp_buf,
820 FILE_ALL_INFO **data)
821 {
822 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
823 struct cifs_search_info info = {0};
824 u16 flags;
825 int rc;
826
827 *resp_buf = NULL;
828 info.endOfSearch = false;
829 if (tcon->unix_ext)
830 info.info_level = SMB_FIND_FILE_UNIX;
831 else if ((tcon->ses->capabilities &
832 tcon->ses->server->vals->cap_nt_find) == 0)
833 info.info_level = SMB_FIND_FILE_INFO_STANDARD;
834 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
835 info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
836 else /* no srvino useful for fallback to some netapp */
837 info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
838
839 flags = CIFS_SEARCH_CLOSE_ALWAYS |
840 CIFS_SEARCH_CLOSE_AT_END |
841 CIFS_SEARCH_BACKUP_SEARCH;
842
843 rc = CIFSFindFirst(xid, tcon, full_path,
844 cifs_sb, NULL, flags, &info, false);
845 if (rc)
846 return rc;
847
848 *resp_buf = (void *)info.ntwrk_buf_start;
849 *data = (FILE_ALL_INFO *)info.srch_entries_start;
850 return 0;
851 }
852
853 static void
854 cifs_set_fattr_ino(int xid,
855 struct cifs_tcon *tcon,
856 struct super_block *sb,
857 struct inode **inode,
858 const char *full_path,
859 FILE_ALL_INFO *data,
860 struct cifs_fattr *fattr)
861 {
862 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
863 struct TCP_Server_Info *server = tcon->ses->server;
864 int rc;
865
866 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
867 if (*inode)
868 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
869 else
870 fattr->cf_uniqueid = iunique(sb, ROOT_I);
871 return;
872 }
873
874 /*
875 * If we have an inode pass a NULL tcon to ensure we don't
876 * make a round trip to the server. This only works for SMB2+.
877 */
878 rc = server->ops->get_srv_inum(xid,
879 *inode ? NULL : tcon,
880 cifs_sb, full_path,
881 &fattr->cf_uniqueid,
882 data);
883 if (rc) {
884 /*
885 * If that fails reuse existing ino or generate one
886 * and disable server ones
887 */
888 if (*inode)
889 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
890 else {
891 fattr->cf_uniqueid = iunique(sb, ROOT_I);
892 cifs_autodisable_serverino(cifs_sb);
893 }
894 return;
895 }
896
897 /* If no errors, check for zero root inode (invalid) */
898 if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
899 cifs_dbg(FYI, "Invalid (0) inodenum\n");
900 if (*inode) {
901 /* reuse */
902 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
903 } else {
904 /* make an ino by hashing the UNC */
905 fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
906 fattr->cf_uniqueid = simple_hashstr(tcon->treeName);
907 }
908 }
909 }
910
911 static inline bool is_inode_cache_good(struct inode *ino)
912 {
913 return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
914 }
915
916 int
917 cifs_get_inode_info(struct inode **inode,
918 const char *full_path,
919 FILE_ALL_INFO *in_data,
920 struct super_block *sb, int xid,
921 const struct cifs_fid *fid)
922 {
923
924 struct cifs_tcon *tcon;
925 struct TCP_Server_Info *server;
926 struct tcon_link *tlink;
927 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
928 bool adjust_tz = false;
929 struct cifs_fattr fattr = {0};
930 bool is_reparse_point = false;
931 FILE_ALL_INFO *data = in_data;
932 FILE_ALL_INFO *tmp_data = NULL;
933 void *smb1_backup_rsp_buf = NULL;
934 int rc = 0;
935 int tmprc = 0;
936 __u32 reparse_tag = 0;
937
938 tlink = cifs_sb_tlink(cifs_sb);
939 if (IS_ERR(tlink))
940 return PTR_ERR(tlink);
941 tcon = tlink_tcon(tlink);
942 server = tcon->ses->server;
943
944 /*
945 * 1. Fetch file metadata if not provided (data)
946 */
947
948 if (!data) {
949 if (is_inode_cache_good(*inode)) {
950 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
951 goto out;
952 }
953 tmp_data = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
954 if (!tmp_data) {
955 rc = -ENOMEM;
956 goto out;
957 }
958 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
959 full_path, tmp_data,
960 &adjust_tz, &is_reparse_point);
961 data = tmp_data;
962 }
963
964 /*
965 * 2. Convert it to internal cifs metadata (fattr)
966 */
967
968 switch (rc) {
969 case 0:
970 /*
971 * If the file is a reparse point, it is more complicated
972 * since we have to check if its reparse tag matches a known
973 * special file type e.g. symlink or fifo or char etc.
974 */
975 if ((le32_to_cpu(data->Attributes) & ATTR_REPARSE) &&
976 server->ops->query_reparse_tag) {
977 rc = server->ops->query_reparse_tag(xid, tcon, cifs_sb,
978 full_path, &reparse_tag);
979 cifs_dbg(FYI, "reparse tag 0x%x\n", reparse_tag);
980 }
981 cifs_all_info_to_fattr(&fattr, data, sb, adjust_tz,
982 is_reparse_point, reparse_tag);
983 break;
984 case -EREMOTE:
985 /* DFS link, no metadata available on this server */
986 cifs_create_dfs_fattr(&fattr, sb);
987 rc = 0;
988 break;
989 case -EACCES:
990 /*
991 * perm errors, try again with backup flags if possible
992 *
993 * For SMB2 and later the backup intent flag
994 * is already sent if needed on open and there
995 * is no path based FindFirst operation to use
996 * to retry with
997 */
998 if (backup_cred(cifs_sb) && is_smb1_server(server)) {
999 /* for easier reading */
1000 FILE_DIRECTORY_INFO *fdi;
1001 SEARCH_ID_FULL_DIR_INFO *si;
1002
1003 rc = cifs_backup_query_path_info(xid, tcon, sb,
1004 full_path,
1005 &smb1_backup_rsp_buf,
1006 &data);
1007 if (rc)
1008 goto out;
1009
1010 fdi = (FILE_DIRECTORY_INFO *)data;
1011 si = (SEARCH_ID_FULL_DIR_INFO *)data;
1012
1013 cifs_dir_info_to_fattr(&fattr, fdi, cifs_sb);
1014 fattr.cf_uniqueid = le64_to_cpu(si->UniqueId);
1015 /* uniqueid set, skip get inum step */
1016 goto handle_mnt_opt;
1017 } else {
1018 /* nothing we can do, bail out */
1019 goto out;
1020 }
1021 break;
1022 default:
1023 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1024 goto out;
1025 }
1026
1027 /*
1028 * 3. Get or update inode number (fattr.cf_uniqueid)
1029 */
1030
1031 cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, &fattr);
1032
1033 /*
1034 * 4. Tweak fattr based on mount options
1035 */
1036
1037 handle_mnt_opt:
1038 /* query for SFU type info if supported and needed */
1039 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
1040 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
1041 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
1042 if (tmprc)
1043 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1044 }
1045
1046 /* fill in 0777 bits from ACL */
1047 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1048 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, true,
1049 full_path, fid);
1050 if (rc == -EREMOTE)
1051 rc = 0;
1052 if (rc) {
1053 cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1054 __func__, rc);
1055 goto out;
1056 }
1057 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1058 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, false,
1059 full_path, fid);
1060 if (rc == -EREMOTE)
1061 rc = 0;
1062 if (rc) {
1063 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1064 __func__, rc);
1065 goto out;
1066 }
1067 }
1068
1069 /* fill in remaining high mode bits e.g. SUID, VTX */
1070 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1071 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
1072
1073 /* check for Minshall+French symlinks */
1074 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1075 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
1076 full_path);
1077 if (tmprc)
1078 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1079 }
1080
1081 /*
1082 * 5. Update inode with final fattr data
1083 */
1084
1085 if (!*inode) {
1086 *inode = cifs_iget(sb, &fattr);
1087 if (!*inode)
1088 rc = -ENOMEM;
1089 } else {
1090 /* we already have inode, update it */
1091
1092 /* if uniqueid is different, return error */
1093 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
1094 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
1095 CIFS_I(*inode)->time = 0; /* force reval */
1096 rc = -ESTALE;
1097 goto out;
1098 }
1099
1100 /* if filetype is different, return error */
1101 if (unlikely(((*inode)->i_mode & S_IFMT) !=
1102 (fattr.cf_mode & S_IFMT))) {
1103 CIFS_I(*inode)->time = 0; /* force reval */
1104 rc = -ESTALE;
1105 goto out;
1106 }
1107
1108 cifs_fattr_to_inode(*inode, &fattr);
1109 }
1110 out:
1111 cifs_buf_release(smb1_backup_rsp_buf);
1112 cifs_put_tlink(tlink);
1113 kfree(tmp_data);
1114 return rc;
1115 }
1116
1117 int
1118 smb311_posix_get_inode_info(struct inode **inode,
1119 const char *full_path,
1120 struct super_block *sb, unsigned int xid)
1121 {
1122 struct cifs_tcon *tcon;
1123 struct tcon_link *tlink;
1124 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1125 bool adjust_tz = false;
1126 struct cifs_fattr fattr = {0};
1127 bool symlink = false;
1128 struct smb311_posix_qinfo *data = NULL;
1129 int rc = 0;
1130 int tmprc = 0;
1131
1132 tlink = cifs_sb_tlink(cifs_sb);
1133 if (IS_ERR(tlink))
1134 return PTR_ERR(tlink);
1135 tcon = tlink_tcon(tlink);
1136
1137 /*
1138 * 1. Fetch file metadata
1139 */
1140
1141 if (is_inode_cache_good(*inode)) {
1142 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1143 goto out;
1144 }
1145 data = kmalloc(sizeof(struct smb311_posix_qinfo), GFP_KERNEL);
1146 if (!data) {
1147 rc = -ENOMEM;
1148 goto out;
1149 }
1150
1151 rc = smb311_posix_query_path_info(xid, tcon, cifs_sb,
1152 full_path, data,
1153 &adjust_tz, &symlink);
1154
1155 /*
1156 * 2. Convert it to internal cifs metadata (fattr)
1157 */
1158
1159 switch (rc) {
1160 case 0:
1161 smb311_posix_info_to_fattr(&fattr, data, sb, adjust_tz, symlink);
1162 break;
1163 case -EREMOTE:
1164 /* DFS link, no metadata available on this server */
1165 cifs_create_dfs_fattr(&fattr, sb);
1166 rc = 0;
1167 break;
1168 case -EACCES:
1169 /*
1170 * For SMB2 and later the backup intent flag
1171 * is already sent if needed on open and there
1172 * is no path based FindFirst operation to use
1173 * to retry with so nothing we can do, bail out
1174 */
1175 goto out;
1176 default:
1177 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1178 goto out;
1179 }
1180
1181
1182 /*
1183 * 3. Tweak fattr based on mount options
1184 */
1185
1186 /* check for Minshall+French symlinks */
1187 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1188 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
1189 full_path);
1190 if (tmprc)
1191 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1192 }
1193
1194 /*
1195 * 4. Update inode with final fattr data
1196 */
1197
1198 if (!*inode) {
1199 *inode = cifs_iget(sb, &fattr);
1200 if (!*inode)
1201 rc = -ENOMEM;
1202 } else {
1203 /* we already have inode, update it */
1204
1205 /* if uniqueid is different, return error */
1206 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
1207 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
1208 CIFS_I(*inode)->time = 0; /* force reval */
1209 rc = -ESTALE;
1210 goto out;
1211 }
1212
1213 /* if filetype is different, return error */
1214 if (unlikely(((*inode)->i_mode & S_IFMT) !=
1215 (fattr.cf_mode & S_IFMT))) {
1216 CIFS_I(*inode)->time = 0; /* force reval */
1217 rc = -ESTALE;
1218 goto out;
1219 }
1220
1221 cifs_fattr_to_inode(*inode, &fattr);
1222 }
1223 out:
1224 cifs_put_tlink(tlink);
1225 kfree(data);
1226 return rc;
1227 }
1228
1229
1230 static const struct inode_operations cifs_ipc_inode_ops = {
1231 .lookup = cifs_lookup,
1232 };
1233
1234 static int
1235 cifs_find_inode(struct inode *inode, void *opaque)
1236 {
1237 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
1238
1239 /* don't match inode with different uniqueid */
1240 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1241 return 0;
1242
1243 /* use createtime like an i_generation field */
1244 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1245 return 0;
1246
1247 /* don't match inode of different type */
1248 if (inode_wrong_type(inode, fattr->cf_mode))
1249 return 0;
1250
1251 /* if it's not a directory or has no dentries, then flag it */
1252 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1253 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1254
1255 return 1;
1256 }
1257
1258 static int
1259 cifs_init_inode(struct inode *inode, void *opaque)
1260 {
1261 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
1262
1263 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1264 CIFS_I(inode)->createtime = fattr->cf_createtime;
1265 return 0;
1266 }
1267
1268 /*
1269 * walk dentry list for an inode and report whether it has aliases that
1270 * are hashed. We use this to determine if a directory inode can actually
1271 * be used.
1272 */
1273 static bool
1274 inode_has_hashed_dentries(struct inode *inode)
1275 {
1276 struct dentry *dentry;
1277
1278 spin_lock(&inode->i_lock);
1279 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1280 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1281 spin_unlock(&inode->i_lock);
1282 return true;
1283 }
1284 }
1285 spin_unlock(&inode->i_lock);
1286 return false;
1287 }
1288
1289 /* Given fattrs, get a corresponding inode */
1290 struct inode *
1291 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1292 {
1293 unsigned long hash;
1294 struct inode *inode;
1295
1296 retry_iget5_locked:
1297 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1298
1299 /* hash down to 32-bits on 32-bit arch */
1300 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1301
1302 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1303 if (inode) {
1304 /* was there a potentially problematic inode collision? */
1305 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1306 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1307
1308 if (inode_has_hashed_dentries(inode)) {
1309 cifs_autodisable_serverino(CIFS_SB(sb));
1310 iput(inode);
1311 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1312 goto retry_iget5_locked;
1313 }
1314 }
1315
1316 cifs_fattr_to_inode(inode, fattr);
1317 if (sb->s_flags & SB_NOATIME)
1318 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1319 if (inode->i_state & I_NEW) {
1320 inode->i_ino = hash;
1321 #ifdef CONFIG_CIFS_FSCACHE
1322 /* initialize per-inode cache cookie pointer */
1323 CIFS_I(inode)->fscache = NULL;
1324 #endif
1325 unlock_new_inode(inode);
1326 }
1327 }
1328
1329 return inode;
1330 }
1331
1332 /* gets root inode */
1333 struct inode *cifs_root_iget(struct super_block *sb)
1334 {
1335 unsigned int xid;
1336 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1337 struct inode *inode = NULL;
1338 long rc;
1339 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1340 char *path = NULL;
1341 int len;
1342
1343 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1344 && cifs_sb->prepath) {
1345 len = strlen(cifs_sb->prepath);
1346 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1347 if (path == NULL)
1348 return ERR_PTR(-ENOMEM);
1349 path[0] = '/';
1350 memcpy(path+1, cifs_sb->prepath, len);
1351 } else {
1352 path = kstrdup("", GFP_KERNEL);
1353 if (path == NULL)
1354 return ERR_PTR(-ENOMEM);
1355 }
1356
1357 xid = get_xid();
1358 if (tcon->unix_ext) {
1359 rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
1360 /* some servers mistakenly claim POSIX support */
1361 if (rc != -EOPNOTSUPP)
1362 goto iget_no_retry;
1363 cifs_dbg(VFS, "server does not support POSIX extensions\n");
1364 tcon->unix_ext = false;
1365 }
1366
1367 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1368 if (tcon->posix_extensions)
1369 rc = smb311_posix_get_inode_info(&inode, path, sb, xid);
1370 else
1371 rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
1372
1373 iget_no_retry:
1374 if (!inode) {
1375 inode = ERR_PTR(rc);
1376 goto out;
1377 }
1378
1379 #ifdef CONFIG_CIFS_FSCACHE
1380 /* populate tcon->resource_id */
1381 tcon->resource_id = CIFS_I(inode)->uniqueid;
1382 #endif
1383
1384 if (rc && tcon->pipe) {
1385 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1386 spin_lock(&inode->i_lock);
1387 inode->i_mode |= S_IFDIR;
1388 set_nlink(inode, 2);
1389 inode->i_op = &cifs_ipc_inode_ops;
1390 inode->i_fop = &simple_dir_operations;
1391 inode->i_uid = cifs_sb->mnt_uid;
1392 inode->i_gid = cifs_sb->mnt_gid;
1393 spin_unlock(&inode->i_lock);
1394 } else if (rc) {
1395 iget_failed(inode);
1396 inode = ERR_PTR(rc);
1397 }
1398
1399 out:
1400 kfree(path);
1401 free_xid(xid);
1402 return inode;
1403 }
1404
1405 int
1406 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1407 char *full_path, __u32 dosattr)
1408 {
1409 bool set_time = false;
1410 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1411 struct TCP_Server_Info *server;
1412 FILE_BASIC_INFO info_buf;
1413
1414 if (attrs == NULL)
1415 return -EINVAL;
1416
1417 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1418 if (!server->ops->set_file_info)
1419 return -ENOSYS;
1420
1421 info_buf.Pad = 0;
1422
1423 if (attrs->ia_valid & ATTR_ATIME) {
1424 set_time = true;
1425 info_buf.LastAccessTime =
1426 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1427 } else
1428 info_buf.LastAccessTime = 0;
1429
1430 if (attrs->ia_valid & ATTR_MTIME) {
1431 set_time = true;
1432 info_buf.LastWriteTime =
1433 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1434 } else
1435 info_buf.LastWriteTime = 0;
1436
1437 /*
1438 * Samba throws this field away, but windows may actually use it.
1439 * Do not set ctime unless other time stamps are changed explicitly
1440 * (i.e. by utimes()) since we would then have a mix of client and
1441 * server times.
1442 */
1443 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1444 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1445 info_buf.ChangeTime =
1446 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1447 } else
1448 info_buf.ChangeTime = 0;
1449
1450 info_buf.CreationTime = 0; /* don't change */
1451 info_buf.Attributes = cpu_to_le32(dosattr);
1452
1453 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1454 }
1455
1456 /*
1457 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1458 * and rename it to a random name that hopefully won't conflict with
1459 * anything else.
1460 */
1461 int
1462 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1463 const unsigned int xid)
1464 {
1465 int oplock = 0;
1466 int rc;
1467 struct cifs_fid fid;
1468 struct cifs_open_parms oparms;
1469 struct inode *inode = d_inode(dentry);
1470 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1471 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1472 struct tcon_link *tlink;
1473 struct cifs_tcon *tcon;
1474 __u32 dosattr, origattr;
1475 FILE_BASIC_INFO *info_buf = NULL;
1476
1477 tlink = cifs_sb_tlink(cifs_sb);
1478 if (IS_ERR(tlink))
1479 return PTR_ERR(tlink);
1480 tcon = tlink_tcon(tlink);
1481
1482 /*
1483 * We cannot rename the file if the server doesn't support
1484 * CAP_INFOLEVEL_PASSTHRU
1485 */
1486 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1487 rc = -EBUSY;
1488 goto out;
1489 }
1490
1491 oparms.tcon = tcon;
1492 oparms.cifs_sb = cifs_sb;
1493 oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
1494 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
1495 oparms.disposition = FILE_OPEN;
1496 oparms.path = full_path;
1497 oparms.fid = &fid;
1498 oparms.reconnect = false;
1499
1500 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1501 if (rc != 0)
1502 goto out;
1503
1504 origattr = cifsInode->cifsAttrs;
1505 if (origattr == 0)
1506 origattr |= ATTR_NORMAL;
1507
1508 dosattr = origattr & ~ATTR_READONLY;
1509 if (dosattr == 0)
1510 dosattr |= ATTR_NORMAL;
1511 dosattr |= ATTR_HIDDEN;
1512
1513 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1514 if (dosattr != origattr) {
1515 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1516 if (info_buf == NULL) {
1517 rc = -ENOMEM;
1518 goto out_close;
1519 }
1520 info_buf->Attributes = cpu_to_le32(dosattr);
1521 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1522 current->tgid);
1523 /* although we would like to mark the file hidden
1524 if that fails we will still try to rename it */
1525 if (!rc)
1526 cifsInode->cifsAttrs = dosattr;
1527 else
1528 dosattr = origattr; /* since not able to change them */
1529 }
1530
1531 /* rename the file */
1532 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1533 cifs_sb->local_nls,
1534 cifs_remap(cifs_sb));
1535 if (rc != 0) {
1536 rc = -EBUSY;
1537 goto undo_setattr;
1538 }
1539
1540 /* try to set DELETE_ON_CLOSE */
1541 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1542 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1543 current->tgid);
1544 /*
1545 * some samba versions return -ENOENT when we try to set the
1546 * file disposition here. Likely a samba bug, but work around
1547 * it for now. This means that some cifsXXX files may hang
1548 * around after they shouldn't.
1549 *
1550 * BB: remove this hack after more servers have the fix
1551 */
1552 if (rc == -ENOENT)
1553 rc = 0;
1554 else if (rc != 0) {
1555 rc = -EBUSY;
1556 goto undo_rename;
1557 }
1558 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1559 }
1560
1561 out_close:
1562 CIFSSMBClose(xid, tcon, fid.netfid);
1563 out:
1564 kfree(info_buf);
1565 cifs_put_tlink(tlink);
1566 return rc;
1567
1568 /*
1569 * reset everything back to the original state. Don't bother
1570 * dealing with errors here since we can't do anything about
1571 * them anyway.
1572 */
1573 undo_rename:
1574 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1575 cifs_sb->local_nls, cifs_remap(cifs_sb));
1576 undo_setattr:
1577 if (dosattr != origattr) {
1578 info_buf->Attributes = cpu_to_le32(origattr);
1579 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1580 current->tgid))
1581 cifsInode->cifsAttrs = origattr;
1582 }
1583
1584 goto out_close;
1585 }
1586
1587 /* copied from fs/nfs/dir.c with small changes */
1588 static void
1589 cifs_drop_nlink(struct inode *inode)
1590 {
1591 spin_lock(&inode->i_lock);
1592 if (inode->i_nlink > 0)
1593 drop_nlink(inode);
1594 spin_unlock(&inode->i_lock);
1595 }
1596
1597 /*
1598 * If d_inode(dentry) is null (usually meaning the cached dentry
1599 * is a negative dentry) then we would attempt a standard SMB delete, but
1600 * if that fails we can not attempt the fall back mechanisms on EACCES
1601 * but will return the EACCES to the caller. Note that the VFS does not call
1602 * unlink on negative dentries currently.
1603 */
1604 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1605 {
1606 int rc = 0;
1607 unsigned int xid;
1608 char *full_path = NULL;
1609 struct inode *inode = d_inode(dentry);
1610 struct cifsInodeInfo *cifs_inode;
1611 struct super_block *sb = dir->i_sb;
1612 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1613 struct tcon_link *tlink;
1614 struct cifs_tcon *tcon;
1615 struct TCP_Server_Info *server;
1616 struct iattr *attrs = NULL;
1617 __u32 dosattr = 0, origattr = 0;
1618
1619 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1620
1621 tlink = cifs_sb_tlink(cifs_sb);
1622 if (IS_ERR(tlink))
1623 return PTR_ERR(tlink);
1624 tcon = tlink_tcon(tlink);
1625 server = tcon->ses->server;
1626
1627 xid = get_xid();
1628
1629 if (tcon->nodelete) {
1630 rc = -EACCES;
1631 goto unlink_out;
1632 }
1633
1634 /* Unlink can be called from rename so we can not take the
1635 * sb->s_vfs_rename_mutex here */
1636 full_path = build_path_from_dentry(dentry);
1637 if (full_path == NULL) {
1638 rc = -ENOMEM;
1639 goto unlink_out;
1640 }
1641
1642 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1643 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1644 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1645 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1646 cifs_remap(cifs_sb));
1647 cifs_dbg(FYI, "posix del rc %d\n", rc);
1648 if ((rc == 0) || (rc == -ENOENT))
1649 goto psx_del_no_retry;
1650 }
1651
1652 retry_std_delete:
1653 if (!server->ops->unlink) {
1654 rc = -ENOSYS;
1655 goto psx_del_no_retry;
1656 }
1657
1658 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1659
1660 psx_del_no_retry:
1661 if (!rc) {
1662 if (inode)
1663 cifs_drop_nlink(inode);
1664 } else if (rc == -ENOENT) {
1665 d_drop(dentry);
1666 } else if (rc == -EBUSY) {
1667 if (server->ops->rename_pending_delete) {
1668 rc = server->ops->rename_pending_delete(full_path,
1669 dentry, xid);
1670 if (rc == 0)
1671 cifs_drop_nlink(inode);
1672 }
1673 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1674 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1675 if (attrs == NULL) {
1676 rc = -ENOMEM;
1677 goto out_reval;
1678 }
1679
1680 /* try to reset dos attributes */
1681 cifs_inode = CIFS_I(inode);
1682 origattr = cifs_inode->cifsAttrs;
1683 if (origattr == 0)
1684 origattr |= ATTR_NORMAL;
1685 dosattr = origattr & ~ATTR_READONLY;
1686 if (dosattr == 0)
1687 dosattr |= ATTR_NORMAL;
1688 dosattr |= ATTR_HIDDEN;
1689
1690 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1691 if (rc != 0)
1692 goto out_reval;
1693
1694 goto retry_std_delete;
1695 }
1696
1697 /* undo the setattr if we errored out and it's needed */
1698 if (rc != 0 && dosattr != 0)
1699 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1700
1701 out_reval:
1702 if (inode) {
1703 cifs_inode = CIFS_I(inode);
1704 cifs_inode->time = 0; /* will force revalidate to get info
1705 when needed */
1706 inode->i_ctime = current_time(inode);
1707 }
1708 dir->i_ctime = dir->i_mtime = current_time(dir);
1709 cifs_inode = CIFS_I(dir);
1710 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1711 unlink_out:
1712 kfree(full_path);
1713 kfree(attrs);
1714 free_xid(xid);
1715 cifs_put_tlink(tlink);
1716 return rc;
1717 }
1718
1719 static int
1720 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1721 const char *full_path, struct cifs_sb_info *cifs_sb,
1722 struct cifs_tcon *tcon, const unsigned int xid)
1723 {
1724 int rc = 0;
1725 struct inode *inode = NULL;
1726
1727 if (tcon->posix_extensions)
1728 rc = smb311_posix_get_inode_info(&inode, full_path, parent->i_sb, xid);
1729 else if (tcon->unix_ext)
1730 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1731 xid);
1732 else
1733 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1734 xid, NULL);
1735
1736 if (rc)
1737 return rc;
1738
1739 /*
1740 * setting nlink not necessary except in cases where we failed to get it
1741 * from the server or was set bogus. Also, since this is a brand new
1742 * inode, no need to grab the i_lock before setting the i_nlink.
1743 */
1744 if (inode->i_nlink < 2)
1745 set_nlink(inode, 2);
1746 mode &= ~current_umask();
1747 /* must turn on setgid bit if parent dir has it */
1748 if (parent->i_mode & S_ISGID)
1749 mode |= S_ISGID;
1750
1751 if (tcon->unix_ext) {
1752 struct cifs_unix_set_info_args args = {
1753 .mode = mode,
1754 .ctime = NO_CHANGE_64,
1755 .atime = NO_CHANGE_64,
1756 .mtime = NO_CHANGE_64,
1757 .device = 0,
1758 };
1759 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1760 args.uid = current_fsuid();
1761 if (parent->i_mode & S_ISGID)
1762 args.gid = parent->i_gid;
1763 else
1764 args.gid = current_fsgid();
1765 } else {
1766 args.uid = INVALID_UID; /* no change */
1767 args.gid = INVALID_GID; /* no change */
1768 }
1769 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1770 cifs_sb->local_nls,
1771 cifs_remap(cifs_sb));
1772 } else {
1773 struct TCP_Server_Info *server = tcon->ses->server;
1774 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1775 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1776 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1777 tcon, xid);
1778 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1779 inode->i_mode = (mode | S_IFDIR);
1780
1781 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1782 inode->i_uid = current_fsuid();
1783 if (inode->i_mode & S_ISGID)
1784 inode->i_gid = parent->i_gid;
1785 else
1786 inode->i_gid = current_fsgid();
1787 }
1788 }
1789 d_instantiate(dentry, inode);
1790 return rc;
1791 }
1792
1793 static int
1794 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1795 const char *full_path, struct cifs_sb_info *cifs_sb,
1796 struct cifs_tcon *tcon, const unsigned int xid)
1797 {
1798 int rc = 0;
1799 u32 oplock = 0;
1800 FILE_UNIX_BASIC_INFO *info = NULL;
1801 struct inode *newinode = NULL;
1802 struct cifs_fattr fattr;
1803
1804 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1805 if (info == NULL) {
1806 rc = -ENOMEM;
1807 goto posix_mkdir_out;
1808 }
1809
1810 mode &= ~current_umask();
1811 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1812 NULL /* netfid */, info, &oplock, full_path,
1813 cifs_sb->local_nls, cifs_remap(cifs_sb));
1814 if (rc == -EOPNOTSUPP)
1815 goto posix_mkdir_out;
1816 else if (rc) {
1817 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1818 d_drop(dentry);
1819 goto posix_mkdir_out;
1820 }
1821
1822 if (info->Type == cpu_to_le32(-1))
1823 /* no return info, go query for it */
1824 goto posix_mkdir_get_info;
1825 /*
1826 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1827 * need to set uid/gid.
1828 */
1829
1830 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1831 cifs_fill_uniqueid(inode->i_sb, &fattr);
1832 newinode = cifs_iget(inode->i_sb, &fattr);
1833 if (!newinode)
1834 goto posix_mkdir_get_info;
1835
1836 d_instantiate(dentry, newinode);
1837
1838 #ifdef CONFIG_CIFS_DEBUG2
1839 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1840 dentry, dentry, newinode);
1841
1842 if (newinode->i_nlink != 2)
1843 cifs_dbg(FYI, "unexpected number of links %d\n",
1844 newinode->i_nlink);
1845 #endif
1846
1847 posix_mkdir_out:
1848 kfree(info);
1849 return rc;
1850 posix_mkdir_get_info:
1851 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1852 xid);
1853 goto posix_mkdir_out;
1854 }
1855
1856 int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
1857 {
1858 int rc = 0;
1859 unsigned int xid;
1860 struct cifs_sb_info *cifs_sb;
1861 struct tcon_link *tlink;
1862 struct cifs_tcon *tcon;
1863 struct TCP_Server_Info *server;
1864 char *full_path;
1865
1866 cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
1867 mode, inode);
1868
1869 cifs_sb = CIFS_SB(inode->i_sb);
1870 tlink = cifs_sb_tlink(cifs_sb);
1871 if (IS_ERR(tlink))
1872 return PTR_ERR(tlink);
1873 tcon = tlink_tcon(tlink);
1874
1875 xid = get_xid();
1876
1877 full_path = build_path_from_dentry(direntry);
1878 if (full_path == NULL) {
1879 rc = -ENOMEM;
1880 goto mkdir_out;
1881 }
1882
1883 server = tcon->ses->server;
1884
1885 if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
1886 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
1887 cifs_sb);
1888 d_drop(direntry); /* for time being always refresh inode info */
1889 goto mkdir_out;
1890 }
1891
1892 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1893 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1894 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1895 tcon, xid);
1896 if (rc != -EOPNOTSUPP)
1897 goto mkdir_out;
1898 }
1899
1900 if (!server->ops->mkdir) {
1901 rc = -ENOSYS;
1902 goto mkdir_out;
1903 }
1904
1905 /* BB add setting the equivalent of mode via CreateX w/ACLs */
1906 rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
1907 if (rc) {
1908 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
1909 d_drop(direntry);
1910 goto mkdir_out;
1911 }
1912
1913 /* TODO: skip this for smb2/smb3 */
1914 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1915 xid);
1916 mkdir_out:
1917 /*
1918 * Force revalidate to get parent dir info when needed since cached
1919 * attributes are invalid now.
1920 */
1921 CIFS_I(inode)->time = 0;
1922 kfree(full_path);
1923 free_xid(xid);
1924 cifs_put_tlink(tlink);
1925 return rc;
1926 }
1927
1928 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1929 {
1930 int rc = 0;
1931 unsigned int xid;
1932 struct cifs_sb_info *cifs_sb;
1933 struct tcon_link *tlink;
1934 struct cifs_tcon *tcon;
1935 struct TCP_Server_Info *server;
1936 char *full_path = NULL;
1937 struct cifsInodeInfo *cifsInode;
1938
1939 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
1940
1941 xid = get_xid();
1942
1943 full_path = build_path_from_dentry(direntry);
1944 if (full_path == NULL) {
1945 rc = -ENOMEM;
1946 goto rmdir_exit;
1947 }
1948
1949 cifs_sb = CIFS_SB(inode->i_sb);
1950 tlink = cifs_sb_tlink(cifs_sb);
1951 if (IS_ERR(tlink)) {
1952 rc = PTR_ERR(tlink);
1953 goto rmdir_exit;
1954 }
1955 tcon = tlink_tcon(tlink);
1956 server = tcon->ses->server;
1957
1958 if (!server->ops->rmdir) {
1959 rc = -ENOSYS;
1960 cifs_put_tlink(tlink);
1961 goto rmdir_exit;
1962 }
1963
1964 if (tcon->nodelete) {
1965 rc = -EACCES;
1966 cifs_put_tlink(tlink);
1967 goto rmdir_exit;
1968 }
1969
1970 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
1971 cifs_put_tlink(tlink);
1972
1973 if (!rc) {
1974 spin_lock(&d_inode(direntry)->i_lock);
1975 i_size_write(d_inode(direntry), 0);
1976 clear_nlink(d_inode(direntry));
1977 spin_unlock(&d_inode(direntry)->i_lock);
1978 }
1979
1980 cifsInode = CIFS_I(d_inode(direntry));
1981 /* force revalidate to go get info when needed */
1982 cifsInode->time = 0;
1983
1984 cifsInode = CIFS_I(inode);
1985 /*
1986 * Force revalidate to get parent dir info when needed since cached
1987 * attributes are invalid now.
1988 */
1989 cifsInode->time = 0;
1990
1991 d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
1992 current_time(inode);
1993
1994 rmdir_exit:
1995 kfree(full_path);
1996 free_xid(xid);
1997 return rc;
1998 }
1999
2000 static int
2001 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2002 const char *from_path, struct dentry *to_dentry,
2003 const char *to_path)
2004 {
2005 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2006 struct tcon_link *tlink;
2007 struct cifs_tcon *tcon;
2008 struct TCP_Server_Info *server;
2009 struct cifs_fid fid;
2010 struct cifs_open_parms oparms;
2011 int oplock, rc;
2012
2013 tlink = cifs_sb_tlink(cifs_sb);
2014 if (IS_ERR(tlink))
2015 return PTR_ERR(tlink);
2016 tcon = tlink_tcon(tlink);
2017 server = tcon->ses->server;
2018
2019 if (!server->ops->rename)
2020 return -ENOSYS;
2021
2022 /* try path-based rename first */
2023 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
2024
2025 /*
2026 * Don't bother with rename by filehandle unless file is busy and
2027 * source. Note that cross directory moves do not work with
2028 * rename by filehandle to various Windows servers.
2029 */
2030 if (rc == 0 || rc != -EBUSY)
2031 goto do_rename_exit;
2032
2033 /* Don't fall back to using SMB on SMB 2+ mount */
2034 if (server->vals->protocol_id != 0)
2035 goto do_rename_exit;
2036
2037 /* open-file renames don't work across directories */
2038 if (to_dentry->d_parent != from_dentry->d_parent)
2039 goto do_rename_exit;
2040
2041 oparms.tcon = tcon;
2042 oparms.cifs_sb = cifs_sb;
2043 /* open the file to be renamed -- we need DELETE perms */
2044 oparms.desired_access = DELETE;
2045 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
2046 oparms.disposition = FILE_OPEN;
2047 oparms.path = from_path;
2048 oparms.fid = &fid;
2049 oparms.reconnect = false;
2050
2051 rc = CIFS_open(xid, &oparms, &oplock, NULL);
2052 if (rc == 0) {
2053 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2054 (const char *) to_dentry->d_name.name,
2055 cifs_sb->local_nls, cifs_remap(cifs_sb));
2056 CIFSSMBClose(xid, tcon, fid.netfid);
2057 }
2058 do_rename_exit:
2059 if (rc == 0)
2060 d_move(from_dentry, to_dentry);
2061 cifs_put_tlink(tlink);
2062 return rc;
2063 }
2064
2065 int
2066 cifs_rename2(struct inode *source_dir, struct dentry *source_dentry,
2067 struct inode *target_dir, struct dentry *target_dentry,
2068 unsigned int flags)
2069 {
2070 char *from_name = NULL;
2071 char *to_name = NULL;
2072 struct cifs_sb_info *cifs_sb;
2073 struct tcon_link *tlink;
2074 struct cifs_tcon *tcon;
2075 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2076 FILE_UNIX_BASIC_INFO *info_buf_target;
2077 unsigned int xid;
2078 int rc, tmprc;
2079
2080 if (flags & ~RENAME_NOREPLACE)
2081 return -EINVAL;
2082
2083 cifs_sb = CIFS_SB(source_dir->i_sb);
2084 tlink = cifs_sb_tlink(cifs_sb);
2085 if (IS_ERR(tlink))
2086 return PTR_ERR(tlink);
2087 tcon = tlink_tcon(tlink);
2088
2089 xid = get_xid();
2090
2091 /*
2092 * we already have the rename sem so we do not need to
2093 * grab it again here to protect the path integrity
2094 */
2095 from_name = build_path_from_dentry(source_dentry);
2096 if (from_name == NULL) {
2097 rc = -ENOMEM;
2098 goto cifs_rename_exit;
2099 }
2100
2101 to_name = build_path_from_dentry(target_dentry);
2102 if (to_name == NULL) {
2103 rc = -ENOMEM;
2104 goto cifs_rename_exit;
2105 }
2106
2107 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2108 to_name);
2109
2110 /*
2111 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2112 */
2113 if (flags & RENAME_NOREPLACE)
2114 goto cifs_rename_exit;
2115
2116 if (rc == -EEXIST && tcon->unix_ext) {
2117 /*
2118 * Are src and dst hardlinks of same inode? We can only tell
2119 * with unix extensions enabled.
2120 */
2121 info_buf_source =
2122 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2123 GFP_KERNEL);
2124 if (info_buf_source == NULL) {
2125 rc = -ENOMEM;
2126 goto cifs_rename_exit;
2127 }
2128
2129 info_buf_target = info_buf_source + 1;
2130 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2131 info_buf_source,
2132 cifs_sb->local_nls,
2133 cifs_remap(cifs_sb));
2134 if (tmprc != 0)
2135 goto unlink_target;
2136
2137 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2138 info_buf_target,
2139 cifs_sb->local_nls,
2140 cifs_remap(cifs_sb));
2141
2142 if (tmprc == 0 && (info_buf_source->UniqueId ==
2143 info_buf_target->UniqueId)) {
2144 /* same file, POSIX says that this is a noop */
2145 rc = 0;
2146 goto cifs_rename_exit;
2147 }
2148 }
2149 /*
2150 * else ... BB we could add the same check for Windows by
2151 * checking the UniqueId via FILE_INTERNAL_INFO
2152 */
2153
2154 unlink_target:
2155 /* Try unlinking the target dentry if it's not negative */
2156 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2157 if (d_is_dir(target_dentry))
2158 tmprc = cifs_rmdir(target_dir, target_dentry);
2159 else
2160 tmprc = cifs_unlink(target_dir, target_dentry);
2161 if (tmprc)
2162 goto cifs_rename_exit;
2163 rc = cifs_do_rename(xid, source_dentry, from_name,
2164 target_dentry, to_name);
2165 }
2166
2167 /* force revalidate to go get info when needed */
2168 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2169
2170 source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
2171 target_dir->i_mtime = current_time(source_dir);
2172
2173 cifs_rename_exit:
2174 kfree(info_buf_source);
2175 kfree(from_name);
2176 kfree(to_name);
2177 free_xid(xid);
2178 cifs_put_tlink(tlink);
2179 return rc;
2180 }
2181
2182 static bool
2183 cifs_inode_needs_reval(struct inode *inode)
2184 {
2185 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2186 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2187
2188 if (cifs_i->time == 0)
2189 return true;
2190
2191 if (CIFS_CACHE_READ(cifs_i))
2192 return false;
2193
2194 if (!lookupCacheEnabled)
2195 return true;
2196
2197 if (!cifs_sb->actimeo)
2198 return true;
2199
2200 if (!time_in_range(jiffies, cifs_i->time,
2201 cifs_i->time + cifs_sb->actimeo))
2202 return true;
2203
2204 /* hardlinked files w/ noserverino get "special" treatment */
2205 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2206 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2207 return true;
2208
2209 return false;
2210 }
2211
2212 /*
2213 * Zap the cache. Called when invalid_mapping flag is set.
2214 */
2215 int
2216 cifs_invalidate_mapping(struct inode *inode)
2217 {
2218 int rc = 0;
2219
2220 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2221 rc = invalidate_inode_pages2(inode->i_mapping);
2222 if (rc)
2223 cifs_dbg(VFS, "%s: Could not invalidate inode %p\n",
2224 __func__, inode);
2225 }
2226
2227 cifs_fscache_reset_inode_cookie(inode);
2228 return rc;
2229 }
2230
2231 /**
2232 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2233 * @word: long word containing the bit lock
2234 */
2235 static int
2236 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2237 {
2238 freezable_schedule_unsafe();
2239 if (signal_pending_state(mode, current))
2240 return -ERESTARTSYS;
2241 return 0;
2242 }
2243
2244 int
2245 cifs_revalidate_mapping(struct inode *inode)
2246 {
2247 int rc;
2248 unsigned long *flags = &CIFS_I(inode)->flags;
2249
2250 /* swapfiles are not supposed to be shared */
2251 if (IS_SWAPFILE(inode))
2252 return 0;
2253
2254 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2255 TASK_KILLABLE);
2256 if (rc)
2257 return rc;
2258
2259 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2260 rc = cifs_invalidate_mapping(inode);
2261 if (rc)
2262 set_bit(CIFS_INO_INVALID_MAPPING, flags);
2263 }
2264
2265 clear_bit_unlock(CIFS_INO_LOCK, flags);
2266 smp_mb__after_atomic();
2267 wake_up_bit(flags, CIFS_INO_LOCK);
2268
2269 return rc;
2270 }
2271
2272 int
2273 cifs_zap_mapping(struct inode *inode)
2274 {
2275 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2276 return cifs_revalidate_mapping(inode);
2277 }
2278
2279 int cifs_revalidate_file_attr(struct file *filp)
2280 {
2281 int rc = 0;
2282 struct inode *inode = file_inode(filp);
2283 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2284
2285 if (!cifs_inode_needs_reval(inode))
2286 return rc;
2287
2288 if (tlink_tcon(cfile->tlink)->unix_ext)
2289 rc = cifs_get_file_info_unix(filp);
2290 else
2291 rc = cifs_get_file_info(filp);
2292
2293 return rc;
2294 }
2295
2296 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2297 {
2298 unsigned int xid;
2299 int rc = 0;
2300 struct inode *inode = d_inode(dentry);
2301 struct super_block *sb = dentry->d_sb;
2302 char *full_path = NULL;
2303 int count = 0;
2304
2305 if (inode == NULL)
2306 return -ENOENT;
2307
2308 if (!cifs_inode_needs_reval(inode))
2309 return rc;
2310
2311 xid = get_xid();
2312
2313 /* can not safely grab the rename sem here if rename calls revalidate
2314 since that would deadlock */
2315 full_path = build_path_from_dentry(dentry);
2316 if (full_path == NULL) {
2317 rc = -ENOMEM;
2318 goto out;
2319 }
2320
2321 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2322 full_path, inode, inode->i_count.counter,
2323 dentry, cifs_get_time(dentry), jiffies);
2324
2325 again:
2326 if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions)
2327 rc = smb311_posix_get_inode_info(&inode, full_path, sb, xid);
2328 else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
2329 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2330 else
2331 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2332 xid, NULL);
2333 if (rc == -EAGAIN && count++ < 10)
2334 goto again;
2335 out:
2336 kfree(full_path);
2337 free_xid(xid);
2338
2339 return rc;
2340 }
2341
2342 int cifs_revalidate_file(struct file *filp)
2343 {
2344 int rc;
2345 struct inode *inode = file_inode(filp);
2346
2347 rc = cifs_revalidate_file_attr(filp);
2348 if (rc)
2349 return rc;
2350
2351 return cifs_revalidate_mapping(inode);
2352 }
2353
2354 /* revalidate a dentry's inode attributes */
2355 int cifs_revalidate_dentry(struct dentry *dentry)
2356 {
2357 int rc;
2358 struct inode *inode = d_inode(dentry);
2359
2360 rc = cifs_revalidate_dentry_attr(dentry);
2361 if (rc)
2362 return rc;
2363
2364 return cifs_revalidate_mapping(inode);
2365 }
2366
2367 int cifs_getattr(const struct path *path, struct kstat *stat,
2368 u32 request_mask, unsigned int flags)
2369 {
2370 struct dentry *dentry = path->dentry;
2371 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2372 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2373 struct inode *inode = d_inode(dentry);
2374 int rc;
2375
2376 /*
2377 * We need to be sure that all dirty pages are written and the server
2378 * has actual ctime, mtime and file length.
2379 */
2380 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2381 !CIFS_CACHE_READ(CIFS_I(inode)) &&
2382 inode->i_mapping && inode->i_mapping->nrpages != 0) {
2383 rc = filemap_fdatawait(inode->i_mapping);
2384 if (rc) {
2385 mapping_set_error(inode->i_mapping, rc);
2386 return rc;
2387 }
2388 }
2389
2390 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2391 CIFS_I(inode)->time = 0; /* force revalidate */
2392
2393 /*
2394 * If the caller doesn't require syncing, only sync if
2395 * necessary (e.g. due to earlier truncate or setattr
2396 * invalidating the cached metadata)
2397 */
2398 if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2399 (CIFS_I(inode)->time == 0)) {
2400 rc = cifs_revalidate_dentry_attr(dentry);
2401 if (rc)
2402 return rc;
2403 }
2404
2405 generic_fillattr(inode, stat);
2406 stat->blksize = cifs_sb->bsize;
2407 stat->ino = CIFS_I(inode)->uniqueid;
2408
2409 /* old CIFS Unix Extensions doesn't return create time */
2410 if (CIFS_I(inode)->createtime) {
2411 stat->result_mask |= STATX_BTIME;
2412 stat->btime =
2413 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2414 }
2415
2416 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2417 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2418 stat->attributes |= STATX_ATTR_COMPRESSED;
2419 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2420 stat->attributes |= STATX_ATTR_ENCRYPTED;
2421
2422 /*
2423 * If on a multiuser mount without unix extensions or cifsacl being
2424 * enabled, and the admin hasn't overridden them, set the ownership
2425 * to the fsuid/fsgid of the current process.
2426 */
2427 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2428 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2429 !tcon->unix_ext) {
2430 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2431 stat->uid = current_fsuid();
2432 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2433 stat->gid = current_fsgid();
2434 }
2435 return 0;
2436 }
2437
2438 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2439 u64 len)
2440 {
2441 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2442 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->vfs_inode.i_sb);
2443 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2444 struct TCP_Server_Info *server = tcon->ses->server;
2445 struct cifsFileInfo *cfile;
2446 int rc;
2447
2448 /*
2449 * We need to be sure that all dirty pages are written as they
2450 * might fill holes on the server.
2451 */
2452 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2453 inode->i_mapping->nrpages != 0) {
2454 rc = filemap_fdatawait(inode->i_mapping);
2455 if (rc) {
2456 mapping_set_error(inode->i_mapping, rc);
2457 return rc;
2458 }
2459 }
2460
2461 cfile = find_readable_file(cifs_i, false);
2462 if (cfile == NULL)
2463 return -EINVAL;
2464
2465 if (server->ops->fiemap) {
2466 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2467 cifsFileInfo_put(cfile);
2468 return rc;
2469 }
2470
2471 cifsFileInfo_put(cfile);
2472 return -ENOTSUPP;
2473 }
2474
2475 int cifs_truncate_page(struct address_space *mapping, loff_t from)
2476 {
2477 pgoff_t index = from >> PAGE_SHIFT;
2478 unsigned offset = from & (PAGE_SIZE - 1);
2479 struct page *page;
2480 int rc = 0;
2481
2482 page = grab_cache_page(mapping, index);
2483 if (!page)
2484 return -ENOMEM;
2485
2486 zero_user_segment(page, offset, PAGE_SIZE);
2487 unlock_page(page);
2488 put_page(page);
2489 return rc;
2490 }
2491
2492 void cifs_setsize(struct inode *inode, loff_t offset)
2493 {
2494 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2495
2496 spin_lock(&inode->i_lock);
2497 i_size_write(inode, offset);
2498 spin_unlock(&inode->i_lock);
2499
2500 /* Cached inode must be refreshed on truncate */
2501 cifs_i->time = 0;
2502 truncate_pagecache(inode, offset);
2503 }
2504
2505 static int
2506 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2507 unsigned int xid, char *full_path)
2508 {
2509 int rc;
2510 struct cifsFileInfo *open_file;
2511 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2512 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2513 struct tcon_link *tlink = NULL;
2514 struct cifs_tcon *tcon = NULL;
2515 struct TCP_Server_Info *server;
2516
2517 /*
2518 * To avoid spurious oplock breaks from server, in the case of
2519 * inodes that we already have open, avoid doing path based
2520 * setting of file size if we can do it by handle.
2521 * This keeps our caching token (oplock) and avoids timeouts
2522 * when the local oplock break takes longer to flush
2523 * writebehind data than the SMB timeout for the SetPathInfo
2524 * request would allow
2525 */
2526 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2527 if (open_file) {
2528 tcon = tlink_tcon(open_file->tlink);
2529 server = tcon->ses->server;
2530 if (server->ops->set_file_size)
2531 rc = server->ops->set_file_size(xid, tcon, open_file,
2532 attrs->ia_size, false);
2533 else
2534 rc = -ENOSYS;
2535 cifsFileInfo_put(open_file);
2536 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2537 } else
2538 rc = -EINVAL;
2539
2540 if (!rc)
2541 goto set_size_out;
2542
2543 if (tcon == NULL) {
2544 tlink = cifs_sb_tlink(cifs_sb);
2545 if (IS_ERR(tlink))
2546 return PTR_ERR(tlink);
2547 tcon = tlink_tcon(tlink);
2548 server = tcon->ses->server;
2549 }
2550
2551 /*
2552 * Set file size by pathname rather than by handle either because no
2553 * valid, writeable file handle for it was found or because there was
2554 * an error setting it by handle.
2555 */
2556 if (server->ops->set_path_size)
2557 rc = server->ops->set_path_size(xid, tcon, full_path,
2558 attrs->ia_size, cifs_sb, false);
2559 else
2560 rc = -ENOSYS;
2561 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2562
2563 if (tlink)
2564 cifs_put_tlink(tlink);
2565
2566 set_size_out:
2567 if (rc == 0) {
2568 cifsInode->server_eof = attrs->ia_size;
2569 cifs_setsize(inode, attrs->ia_size);
2570 /*
2571 * i_blocks is not related to (i_size / i_blksize), but instead
2572 * 512 byte (2**9) size is required for calculating num blocks.
2573 * Until we can query the server for actual allocation size,
2574 * this is best estimate we have for blocks allocated for a file
2575 * Number of blocks must be rounded up so size 1 is not 0 blocks
2576 */
2577 inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2578
2579 /*
2580 * The man page of truncate says if the size changed,
2581 * then the st_ctime and st_mtime fields for the file
2582 * are updated.
2583 */
2584 attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2585 attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2586
2587 cifs_truncate_page(inode->i_mapping, inode->i_size);
2588 }
2589
2590 return rc;
2591 }
2592
2593 static int
2594 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2595 {
2596 int rc;
2597 unsigned int xid;
2598 char *full_path = NULL;
2599 struct inode *inode = d_inode(direntry);
2600 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2601 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2602 struct tcon_link *tlink;
2603 struct cifs_tcon *pTcon;
2604 struct cifs_unix_set_info_args *args = NULL;
2605 struct cifsFileInfo *open_file;
2606
2607 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2608 direntry, attrs->ia_valid);
2609
2610 xid = get_xid();
2611
2612 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2613 attrs->ia_valid |= ATTR_FORCE;
2614
2615 rc = setattr_prepare(direntry, attrs);
2616 if (rc < 0)
2617 goto out;
2618
2619 full_path = build_path_from_dentry(direntry);
2620 if (full_path == NULL) {
2621 rc = -ENOMEM;
2622 goto out;
2623 }
2624
2625 /*
2626 * Attempt to flush data before changing attributes. We need to do
2627 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2628 * ownership or mode then we may also need to do this. Here, we take
2629 * the safe way out and just do the flush on all setattr requests. If
2630 * the flush returns error, store it to report later and continue.
2631 *
2632 * BB: This should be smarter. Why bother flushing pages that
2633 * will be truncated anyway? Also, should we error out here if
2634 * the flush returns error?
2635 */
2636 rc = filemap_write_and_wait(inode->i_mapping);
2637 if (is_interrupt_error(rc)) {
2638 rc = -ERESTARTSYS;
2639 goto out;
2640 }
2641
2642 mapping_set_error(inode->i_mapping, rc);
2643 rc = 0;
2644
2645 if (attrs->ia_valid & ATTR_SIZE) {
2646 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2647 if (rc != 0)
2648 goto out;
2649 }
2650
2651 /* skip mode change if it's just for clearing setuid/setgid */
2652 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2653 attrs->ia_valid &= ~ATTR_MODE;
2654
2655 args = kmalloc(sizeof(*args), GFP_KERNEL);
2656 if (args == NULL) {
2657 rc = -ENOMEM;
2658 goto out;
2659 }
2660
2661 /* set up the struct */
2662 if (attrs->ia_valid & ATTR_MODE)
2663 args->mode = attrs->ia_mode;
2664 else
2665 args->mode = NO_CHANGE_64;
2666
2667 if (attrs->ia_valid & ATTR_UID)
2668 args->uid = attrs->ia_uid;
2669 else
2670 args->uid = INVALID_UID; /* no change */
2671
2672 if (attrs->ia_valid & ATTR_GID)
2673 args->gid = attrs->ia_gid;
2674 else
2675 args->gid = INVALID_GID; /* no change */
2676
2677 if (attrs->ia_valid & ATTR_ATIME)
2678 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2679 else
2680 args->atime = NO_CHANGE_64;
2681
2682 if (attrs->ia_valid & ATTR_MTIME)
2683 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2684 else
2685 args->mtime = NO_CHANGE_64;
2686
2687 if (attrs->ia_valid & ATTR_CTIME)
2688 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2689 else
2690 args->ctime = NO_CHANGE_64;
2691
2692 args->device = 0;
2693 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2694 if (open_file) {
2695 u16 nfid = open_file->fid.netfid;
2696 u32 npid = open_file->pid;
2697 pTcon = tlink_tcon(open_file->tlink);
2698 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2699 cifsFileInfo_put(open_file);
2700 } else {
2701 tlink = cifs_sb_tlink(cifs_sb);
2702 if (IS_ERR(tlink)) {
2703 rc = PTR_ERR(tlink);
2704 goto out;
2705 }
2706 pTcon = tlink_tcon(tlink);
2707 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2708 cifs_sb->local_nls,
2709 cifs_remap(cifs_sb));
2710 cifs_put_tlink(tlink);
2711 }
2712
2713 if (rc)
2714 goto out;
2715
2716 if ((attrs->ia_valid & ATTR_SIZE) &&
2717 attrs->ia_size != i_size_read(inode))
2718 truncate_setsize(inode, attrs->ia_size);
2719
2720 setattr_copy(inode, attrs);
2721 mark_inode_dirty(inode);
2722
2723 /* force revalidate when any of these times are set since some
2724 of the fs types (eg ext3, fat) do not have fine enough
2725 time granularity to match protocol, and we do not have a
2726 a way (yet) to query the server fs's time granularity (and
2727 whether it rounds times down).
2728 */
2729 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2730 cifsInode->time = 0;
2731 out:
2732 kfree(args);
2733 kfree(full_path);
2734 free_xid(xid);
2735 return rc;
2736 }
2737
2738 static int
2739 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2740 {
2741 unsigned int xid;
2742 kuid_t uid = INVALID_UID;
2743 kgid_t gid = INVALID_GID;
2744 struct inode *inode = d_inode(direntry);
2745 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2746 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2747 struct cifsFileInfo *wfile;
2748 struct cifs_tcon *tcon;
2749 char *full_path = NULL;
2750 int rc = -EACCES;
2751 __u32 dosattr = 0;
2752 __u64 mode = NO_CHANGE_64;
2753
2754 xid = get_xid();
2755
2756 cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
2757 direntry, attrs->ia_valid);
2758
2759 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2760 attrs->ia_valid |= ATTR_FORCE;
2761
2762 rc = setattr_prepare(direntry, attrs);
2763 if (rc < 0) {
2764 free_xid(xid);
2765 return rc;
2766 }
2767
2768 full_path = build_path_from_dentry(direntry);
2769 if (full_path == NULL) {
2770 rc = -ENOMEM;
2771 free_xid(xid);
2772 return rc;
2773 }
2774
2775 /*
2776 * Attempt to flush data before changing attributes. We need to do
2777 * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
2778 * returns error, store it to report later and continue.
2779 *
2780 * BB: This should be smarter. Why bother flushing pages that
2781 * will be truncated anyway? Also, should we error out here if
2782 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
2783 */
2784 if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
2785 rc = filemap_write_and_wait(inode->i_mapping);
2786 if (is_interrupt_error(rc)) {
2787 rc = -ERESTARTSYS;
2788 goto cifs_setattr_exit;
2789 }
2790 mapping_set_error(inode->i_mapping, rc);
2791 }
2792
2793 rc = 0;
2794
2795 if ((attrs->ia_valid & ATTR_MTIME) &&
2796 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2797 rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
2798 if (!rc) {
2799 tcon = tlink_tcon(wfile->tlink);
2800 rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
2801 cifsFileInfo_put(wfile);
2802 if (rc)
2803 goto cifs_setattr_exit;
2804 } else if (rc != -EBADF)
2805 goto cifs_setattr_exit;
2806 else
2807 rc = 0;
2808 }
2809
2810 if (attrs->ia_valid & ATTR_SIZE) {
2811 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2812 if (rc != 0)
2813 goto cifs_setattr_exit;
2814 }
2815
2816 if (attrs->ia_valid & ATTR_UID)
2817 uid = attrs->ia_uid;
2818
2819 if (attrs->ia_valid & ATTR_GID)
2820 gid = attrs->ia_gid;
2821
2822 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2823 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
2824 if (uid_valid(uid) || gid_valid(gid)) {
2825 rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
2826 uid, gid);
2827 if (rc) {
2828 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2829 __func__, rc);
2830 goto cifs_setattr_exit;
2831 }
2832 }
2833 } else
2834 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
2835 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
2836
2837 /* skip mode change if it's just for clearing setuid/setgid */
2838 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2839 attrs->ia_valid &= ~ATTR_MODE;
2840
2841 if (attrs->ia_valid & ATTR_MODE) {
2842 mode = attrs->ia_mode;
2843 rc = 0;
2844 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2845 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
2846 rc = id_mode_to_cifs_acl(inode, full_path, mode,
2847 INVALID_UID, INVALID_GID);
2848 if (rc) {
2849 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2850 __func__, rc);
2851 goto cifs_setattr_exit;
2852 }
2853 } else
2854 if (((mode & S_IWUGO) == 0) &&
2855 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
2856
2857 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2858
2859 /* fix up mode if we're not using dynperm */
2860 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2861 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2862 } else if ((mode & S_IWUGO) &&
2863 (cifsInode->cifsAttrs & ATTR_READONLY)) {
2864
2865 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2866 /* Attributes of 0 are ignored */
2867 if (dosattr == 0)
2868 dosattr |= ATTR_NORMAL;
2869
2870 /* reset local inode permissions to normal */
2871 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2872 attrs->ia_mode &= ~(S_IALLUGO);
2873 if (S_ISDIR(inode->i_mode))
2874 attrs->ia_mode |=
2875 cifs_sb->mnt_dir_mode;
2876 else
2877 attrs->ia_mode |=
2878 cifs_sb->mnt_file_mode;
2879 }
2880 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2881 /* ignore mode change - ATTR_READONLY hasn't changed */
2882 attrs->ia_valid &= ~ATTR_MODE;
2883 }
2884 }
2885
2886 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
2887 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
2888 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2889 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
2890
2891 /* Even if error on time set, no sense failing the call if
2892 the server would set the time to a reasonable value anyway,
2893 and this check ensures that we are not being called from
2894 sys_utimes in which case we ought to fail the call back to
2895 the user when the server rejects the call */
2896 if ((rc) && (attrs->ia_valid &
2897 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
2898 rc = 0;
2899 }
2900
2901 /* do not need local check to inode_check_ok since the server does
2902 that */
2903 if (rc)
2904 goto cifs_setattr_exit;
2905
2906 if ((attrs->ia_valid & ATTR_SIZE) &&
2907 attrs->ia_size != i_size_read(inode))
2908 truncate_setsize(inode, attrs->ia_size);
2909
2910 setattr_copy(inode, attrs);
2911 mark_inode_dirty(inode);
2912
2913 cifs_setattr_exit:
2914 kfree(full_path);
2915 free_xid(xid);
2916 return rc;
2917 }
2918
2919 int
2920 cifs_setattr(struct dentry *direntry, struct iattr *attrs)
2921 {
2922 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
2923 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
2924 int rc, retries = 0;
2925
2926 do {
2927 if (pTcon->unix_ext)
2928 rc = cifs_setattr_unix(direntry, attrs);
2929 else
2930 rc = cifs_setattr_nounix(direntry, attrs);
2931 retries++;
2932 } while (is_retryable_error(rc) && retries < 2);
2933
2934 /* BB: add cifs_setattr_legacy for really old servers */
2935 return rc;
2936 }
2937
2938 #if 0
2939 void cifs_delete_inode(struct inode *inode)
2940 {
2941 cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode);
2942 /* may have to add back in if and when safe distributed caching of
2943 directories added e.g. via FindNotify */
2944 }
2945 #endif
2946