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