]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/cifs/inode.c
sched/headers: Prepare to move signal wakeup & sigpending methods from <linux/sched...
[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 fattr->cf_atime = CURRENT_TIME;
326 fattr->cf_ctime = CURRENT_TIME;
327 fattr->cf_mtime = CURRENT_TIME;
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->local_nls,
567 cifs_remap(cifs_sb));
568 cifs_put_tlink(tlink);
569 if (rc < 0)
570 return (int)rc;
571 else if (rc > 3) {
572 mode = le32_to_cpu(*((__le32 *)ea_value));
573 fattr->cf_mode &= ~SFBITS_MASK;
574 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
575 mode, fattr->cf_mode);
576 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
577 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
578 }
579
580 return 0;
581 #else
582 return -EOPNOTSUPP;
583 #endif
584 }
585
586 /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
587 static void
588 cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
589 struct cifs_sb_info *cifs_sb, bool adjust_tz,
590 bool symlink)
591 {
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 fattr->cf_atime = CURRENT_TIME;
603
604 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
605 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
606
607 if (adjust_tz) {
608 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
609 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
610 }
611
612 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
613 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
614 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
615
616 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
617
618 if (symlink) {
619 fattr->cf_mode = S_IFLNK;
620 fattr->cf_dtype = DT_LNK;
621 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
622 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
623 fattr->cf_dtype = DT_DIR;
624 /*
625 * Server can return wrong NumberOfLinks value for directories
626 * when Unix extensions are disabled - fake it.
627 */
628 if (!tcon->unix_ext)
629 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
630 } else {
631 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
632 fattr->cf_dtype = DT_REG;
633
634 /* clear write bits if ATTR_READONLY is set */
635 if (fattr->cf_cifsattrs & ATTR_READONLY)
636 fattr->cf_mode &= ~(S_IWUGO);
637
638 /*
639 * Don't accept zero nlink from non-unix servers unless
640 * delete is pending. Instead mark it as unknown.
641 */
642 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
643 !info->DeletePending) {
644 cifs_dbg(1, "bogus file nlink value %u\n",
645 fattr->cf_nlink);
646 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
647 }
648 }
649
650 fattr->cf_uid = cifs_sb->mnt_uid;
651 fattr->cf_gid = cifs_sb->mnt_gid;
652 }
653
654 static int
655 cifs_get_file_info(struct file *filp)
656 {
657 int rc;
658 unsigned int xid;
659 FILE_ALL_INFO find_data;
660 struct cifs_fattr fattr;
661 struct inode *inode = file_inode(filp);
662 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
663 struct cifsFileInfo *cfile = filp->private_data;
664 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
665 struct TCP_Server_Info *server = tcon->ses->server;
666
667 if (!server->ops->query_file_info)
668 return -ENOSYS;
669
670 xid = get_xid();
671 rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
672 switch (rc) {
673 case 0:
674 cifs_all_info_to_fattr(&fattr, &find_data, cifs_sb, false,
675 false);
676 break;
677 case -EREMOTE:
678 cifs_create_dfs_fattr(&fattr, inode->i_sb);
679 rc = 0;
680 break;
681 case -EOPNOTSUPP:
682 case -EINVAL:
683 /*
684 * FIXME: legacy server -- fall back to path-based call?
685 * for now, just skip revalidating and mark inode for
686 * immediate reval.
687 */
688 rc = 0;
689 CIFS_I(inode)->time = 0;
690 default:
691 goto cgfi_exit;
692 }
693
694 /*
695 * don't bother with SFU junk here -- just mark inode as needing
696 * revalidation.
697 */
698 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
699 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
700 cifs_fattr_to_inode(inode, &fattr);
701 cgfi_exit:
702 free_xid(xid);
703 return rc;
704 }
705
706 int
707 cifs_get_inode_info(struct inode **inode, const char *full_path,
708 FILE_ALL_INFO *data, struct super_block *sb, int xid,
709 const struct cifs_fid *fid)
710 {
711 bool validinum = false;
712 __u16 srchflgs;
713 int rc = 0, tmprc = ENOSYS;
714 struct cifs_tcon *tcon;
715 struct TCP_Server_Info *server;
716 struct tcon_link *tlink;
717 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
718 char *buf = NULL;
719 bool adjust_tz = false;
720 struct cifs_fattr fattr;
721 struct cifs_search_info *srchinf = NULL;
722 bool symlink = false;
723
724 tlink = cifs_sb_tlink(cifs_sb);
725 if (IS_ERR(tlink))
726 return PTR_ERR(tlink);
727 tcon = tlink_tcon(tlink);
728 server = tcon->ses->server;
729
730 cifs_dbg(FYI, "Getting info on %s\n", full_path);
731
732 if ((data == NULL) && (*inode != NULL)) {
733 if (CIFS_CACHE_READ(CIFS_I(*inode))) {
734 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
735 goto cgii_exit;
736 }
737 }
738
739 /* if inode info is not passed, get it from server */
740 if (data == NULL) {
741 if (!server->ops->query_path_info) {
742 rc = -ENOSYS;
743 goto cgii_exit;
744 }
745 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
746 if (buf == NULL) {
747 rc = -ENOMEM;
748 goto cgii_exit;
749 }
750 data = (FILE_ALL_INFO *)buf;
751 rc = server->ops->query_path_info(xid, tcon, cifs_sb, full_path,
752 data, &adjust_tz, &symlink);
753 }
754
755 if (!rc) {
756 cifs_all_info_to_fattr(&fattr, data, cifs_sb, adjust_tz,
757 symlink);
758 } else if (rc == -EREMOTE) {
759 cifs_create_dfs_fattr(&fattr, sb);
760 rc = 0;
761 } else if (rc == -EACCES && backup_cred(cifs_sb)) {
762 srchinf = kzalloc(sizeof(struct cifs_search_info),
763 GFP_KERNEL);
764 if (srchinf == NULL) {
765 rc = -ENOMEM;
766 goto cgii_exit;
767 }
768
769 srchinf->endOfSearch = false;
770 srchinf->info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
771
772 srchflgs = CIFS_SEARCH_CLOSE_ALWAYS |
773 CIFS_SEARCH_CLOSE_AT_END |
774 CIFS_SEARCH_BACKUP_SEARCH;
775
776 rc = CIFSFindFirst(xid, tcon, full_path,
777 cifs_sb, NULL, srchflgs, srchinf, false);
778 if (!rc) {
779 data =
780 (FILE_ALL_INFO *)srchinf->srch_entries_start;
781
782 cifs_dir_info_to_fattr(&fattr,
783 (FILE_DIRECTORY_INFO *)data, cifs_sb);
784 fattr.cf_uniqueid = le64_to_cpu(
785 ((SEARCH_ID_FULL_DIR_INFO *)data)->UniqueId);
786 validinum = true;
787
788 cifs_buf_release(srchinf->ntwrk_buf_start);
789 }
790 kfree(srchinf);
791 if (rc)
792 goto cgii_exit;
793 } else
794 goto cgii_exit;
795
796 /*
797 * If an inode wasn't passed in, then get the inode number
798 *
799 * Is an i_ino of zero legal? Can we use that to check if the server
800 * supports returning inode numbers? Are there other sanity checks we
801 * can use to ensure that the server is really filling in that field?
802 */
803 if (*inode == NULL) {
804 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
805 if (validinum == false) {
806 if (server->ops->get_srv_inum)
807 tmprc = server->ops->get_srv_inum(xid,
808 tcon, cifs_sb, full_path,
809 &fattr.cf_uniqueid, data);
810 if (tmprc) {
811 cifs_dbg(FYI, "GetSrvInodeNum rc %d\n",
812 tmprc);
813 fattr.cf_uniqueid = iunique(sb, ROOT_I);
814 cifs_autodisable_serverino(cifs_sb);
815 }
816 }
817 } else
818 fattr.cf_uniqueid = iunique(sb, ROOT_I);
819 } else {
820 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
821 validinum == false && server->ops->get_srv_inum) {
822 /*
823 * Pass a NULL tcon to ensure we don't make a round
824 * trip to the server. This only works for SMB2+.
825 */
826 tmprc = server->ops->get_srv_inum(xid,
827 NULL, cifs_sb, full_path,
828 &fattr.cf_uniqueid, data);
829 if (tmprc)
830 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
831 } else
832 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
833 }
834
835 /* query for SFU type info if supported and needed */
836 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
837 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
838 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
839 if (tmprc)
840 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
841 }
842
843 #ifdef CONFIG_CIFS_ACL
844 /* fill in 0777 bits from ACL */
845 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
846 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, full_path, fid);
847 if (rc) {
848 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
849 __func__, rc);
850 goto cgii_exit;
851 }
852 }
853 #endif /* CONFIG_CIFS_ACL */
854
855 /* fill in remaining high mode bits e.g. SUID, VTX */
856 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
857 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
858
859 /* check for Minshall+French symlinks */
860 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
861 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
862 full_path);
863 if (tmprc)
864 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
865 }
866
867 if (!*inode) {
868 *inode = cifs_iget(sb, &fattr);
869 if (!*inode)
870 rc = -ENOMEM;
871 } else {
872 /* we already have inode, update it */
873
874 /* if uniqueid is different, return error */
875 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
876 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
877 rc = -ESTALE;
878 goto cgii_exit;
879 }
880
881 /* if filetype is different, return error */
882 if (unlikely(((*inode)->i_mode & S_IFMT) !=
883 (fattr.cf_mode & S_IFMT))) {
884 rc = -ESTALE;
885 goto cgii_exit;
886 }
887
888 cifs_fattr_to_inode(*inode, &fattr);
889 }
890
891 cgii_exit:
892 kfree(buf);
893 cifs_put_tlink(tlink);
894 return rc;
895 }
896
897 static const struct inode_operations cifs_ipc_inode_ops = {
898 .lookup = cifs_lookup,
899 };
900
901 static int
902 cifs_find_inode(struct inode *inode, void *opaque)
903 {
904 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
905
906 /* don't match inode with different uniqueid */
907 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
908 return 0;
909
910 /* use createtime like an i_generation field */
911 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
912 return 0;
913
914 /* don't match inode of different type */
915 if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
916 return 0;
917
918 /* if it's not a directory or has no dentries, then flag it */
919 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
920 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
921
922 return 1;
923 }
924
925 static int
926 cifs_init_inode(struct inode *inode, void *opaque)
927 {
928 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
929
930 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
931 CIFS_I(inode)->createtime = fattr->cf_createtime;
932 return 0;
933 }
934
935 /*
936 * walk dentry list for an inode and report whether it has aliases that
937 * are hashed. We use this to determine if a directory inode can actually
938 * be used.
939 */
940 static bool
941 inode_has_hashed_dentries(struct inode *inode)
942 {
943 struct dentry *dentry;
944
945 spin_lock(&inode->i_lock);
946 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
947 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
948 spin_unlock(&inode->i_lock);
949 return true;
950 }
951 }
952 spin_unlock(&inode->i_lock);
953 return false;
954 }
955
956 /* Given fattrs, get a corresponding inode */
957 struct inode *
958 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
959 {
960 unsigned long hash;
961 struct inode *inode;
962
963 retry_iget5_locked:
964 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
965
966 /* hash down to 32-bits on 32-bit arch */
967 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
968
969 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
970 if (inode) {
971 /* was there a potentially problematic inode collision? */
972 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
973 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
974
975 if (inode_has_hashed_dentries(inode)) {
976 cifs_autodisable_serverino(CIFS_SB(sb));
977 iput(inode);
978 fattr->cf_uniqueid = iunique(sb, ROOT_I);
979 goto retry_iget5_locked;
980 }
981 }
982
983 cifs_fattr_to_inode(inode, fattr);
984 if (sb->s_flags & MS_NOATIME)
985 inode->i_flags |= S_NOATIME | S_NOCMTIME;
986 if (inode->i_state & I_NEW) {
987 inode->i_ino = hash;
988 #ifdef CONFIG_CIFS_FSCACHE
989 /* initialize per-inode cache cookie pointer */
990 CIFS_I(inode)->fscache = NULL;
991 #endif
992 unlock_new_inode(inode);
993 }
994 }
995
996 return inode;
997 }
998
999 /* gets root inode */
1000 struct inode *cifs_root_iget(struct super_block *sb)
1001 {
1002 unsigned int xid;
1003 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1004 struct inode *inode = NULL;
1005 long rc;
1006 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1007 char *path = NULL;
1008 int len;
1009
1010 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1011 && cifs_sb->prepath) {
1012 len = strlen(cifs_sb->prepath);
1013 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1014 if (path == NULL)
1015 return ERR_PTR(-ENOMEM);
1016 path[0] = '/';
1017 memcpy(path+1, cifs_sb->prepath, len);
1018 } else {
1019 path = kstrdup("", GFP_KERNEL);
1020 if (path == NULL)
1021 return ERR_PTR(-ENOMEM);
1022 }
1023
1024 xid = get_xid();
1025 if (tcon->unix_ext) {
1026 rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
1027 /* some servers mistakenly claim POSIX support */
1028 if (rc != -EOPNOTSUPP)
1029 goto iget_no_retry;
1030 cifs_dbg(VFS, "server does not support POSIX extensions");
1031 tcon->unix_ext = false;
1032 }
1033
1034 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1035 rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
1036
1037 iget_no_retry:
1038 if (!inode) {
1039 inode = ERR_PTR(rc);
1040 goto out;
1041 }
1042
1043 #ifdef CONFIG_CIFS_FSCACHE
1044 /* populate tcon->resource_id */
1045 tcon->resource_id = CIFS_I(inode)->uniqueid;
1046 #endif
1047
1048 if (rc && tcon->ipc) {
1049 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1050 spin_lock(&inode->i_lock);
1051 inode->i_mode |= S_IFDIR;
1052 set_nlink(inode, 2);
1053 inode->i_op = &cifs_ipc_inode_ops;
1054 inode->i_fop = &simple_dir_operations;
1055 inode->i_uid = cifs_sb->mnt_uid;
1056 inode->i_gid = cifs_sb->mnt_gid;
1057 spin_unlock(&inode->i_lock);
1058 } else if (rc) {
1059 iget_failed(inode);
1060 inode = ERR_PTR(rc);
1061 }
1062
1063 out:
1064 kfree(path);
1065 /* can not call macro free_xid here since in a void func
1066 * TODO: This is no longer true
1067 */
1068 _free_xid(xid);
1069 return inode;
1070 }
1071
1072 int
1073 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1074 char *full_path, __u32 dosattr)
1075 {
1076 bool set_time = false;
1077 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1078 struct TCP_Server_Info *server;
1079 FILE_BASIC_INFO info_buf;
1080
1081 if (attrs == NULL)
1082 return -EINVAL;
1083
1084 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1085 if (!server->ops->set_file_info)
1086 return -ENOSYS;
1087
1088 if (attrs->ia_valid & ATTR_ATIME) {
1089 set_time = true;
1090 info_buf.LastAccessTime =
1091 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1092 } else
1093 info_buf.LastAccessTime = 0;
1094
1095 if (attrs->ia_valid & ATTR_MTIME) {
1096 set_time = true;
1097 info_buf.LastWriteTime =
1098 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1099 } else
1100 info_buf.LastWriteTime = 0;
1101
1102 /*
1103 * Samba throws this field away, but windows may actually use it.
1104 * Do not set ctime unless other time stamps are changed explicitly
1105 * (i.e. by utimes()) since we would then have a mix of client and
1106 * server times.
1107 */
1108 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1109 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1110 info_buf.ChangeTime =
1111 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1112 } else
1113 info_buf.ChangeTime = 0;
1114
1115 info_buf.CreationTime = 0; /* don't change */
1116 info_buf.Attributes = cpu_to_le32(dosattr);
1117
1118 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1119 }
1120
1121 /*
1122 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1123 * and rename it to a random name that hopefully won't conflict with
1124 * anything else.
1125 */
1126 int
1127 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1128 const unsigned int xid)
1129 {
1130 int oplock = 0;
1131 int rc;
1132 struct cifs_fid fid;
1133 struct cifs_open_parms oparms;
1134 struct inode *inode = d_inode(dentry);
1135 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1136 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1137 struct tcon_link *tlink;
1138 struct cifs_tcon *tcon;
1139 __u32 dosattr, origattr;
1140 FILE_BASIC_INFO *info_buf = NULL;
1141
1142 tlink = cifs_sb_tlink(cifs_sb);
1143 if (IS_ERR(tlink))
1144 return PTR_ERR(tlink);
1145 tcon = tlink_tcon(tlink);
1146
1147 /*
1148 * We cannot rename the file if the server doesn't support
1149 * CAP_INFOLEVEL_PASSTHRU
1150 */
1151 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1152 rc = -EBUSY;
1153 goto out;
1154 }
1155
1156 oparms.tcon = tcon;
1157 oparms.cifs_sb = cifs_sb;
1158 oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
1159 oparms.create_options = CREATE_NOT_DIR;
1160 oparms.disposition = FILE_OPEN;
1161 oparms.path = full_path;
1162 oparms.fid = &fid;
1163 oparms.reconnect = false;
1164
1165 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1166 if (rc != 0)
1167 goto out;
1168
1169 origattr = cifsInode->cifsAttrs;
1170 if (origattr == 0)
1171 origattr |= ATTR_NORMAL;
1172
1173 dosattr = origattr & ~ATTR_READONLY;
1174 if (dosattr == 0)
1175 dosattr |= ATTR_NORMAL;
1176 dosattr |= ATTR_HIDDEN;
1177
1178 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1179 if (dosattr != origattr) {
1180 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1181 if (info_buf == NULL) {
1182 rc = -ENOMEM;
1183 goto out_close;
1184 }
1185 info_buf->Attributes = cpu_to_le32(dosattr);
1186 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1187 current->tgid);
1188 /* although we would like to mark the file hidden
1189 if that fails we will still try to rename it */
1190 if (!rc)
1191 cifsInode->cifsAttrs = dosattr;
1192 else
1193 dosattr = origattr; /* since not able to change them */
1194 }
1195
1196 /* rename the file */
1197 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1198 cifs_sb->local_nls,
1199 cifs_remap(cifs_sb));
1200 if (rc != 0) {
1201 rc = -EBUSY;
1202 goto undo_setattr;
1203 }
1204
1205 /* try to set DELETE_ON_CLOSE */
1206 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1207 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1208 current->tgid);
1209 /*
1210 * some samba versions return -ENOENT when we try to set the
1211 * file disposition here. Likely a samba bug, but work around
1212 * it for now. This means that some cifsXXX files may hang
1213 * around after they shouldn't.
1214 *
1215 * BB: remove this hack after more servers have the fix
1216 */
1217 if (rc == -ENOENT)
1218 rc = 0;
1219 else if (rc != 0) {
1220 rc = -EBUSY;
1221 goto undo_rename;
1222 }
1223 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1224 }
1225
1226 out_close:
1227 CIFSSMBClose(xid, tcon, fid.netfid);
1228 out:
1229 kfree(info_buf);
1230 cifs_put_tlink(tlink);
1231 return rc;
1232
1233 /*
1234 * reset everything back to the original state. Don't bother
1235 * dealing with errors here since we can't do anything about
1236 * them anyway.
1237 */
1238 undo_rename:
1239 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1240 cifs_sb->local_nls, cifs_remap(cifs_sb));
1241 undo_setattr:
1242 if (dosattr != origattr) {
1243 info_buf->Attributes = cpu_to_le32(origattr);
1244 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1245 current->tgid))
1246 cifsInode->cifsAttrs = origattr;
1247 }
1248
1249 goto out_close;
1250 }
1251
1252 /* copied from fs/nfs/dir.c with small changes */
1253 static void
1254 cifs_drop_nlink(struct inode *inode)
1255 {
1256 spin_lock(&inode->i_lock);
1257 if (inode->i_nlink > 0)
1258 drop_nlink(inode);
1259 spin_unlock(&inode->i_lock);
1260 }
1261
1262 /*
1263 * If d_inode(dentry) is null (usually meaning the cached dentry
1264 * is a negative dentry) then we would attempt a standard SMB delete, but
1265 * if that fails we can not attempt the fall back mechanisms on EACCESS
1266 * but will return the EACCESS to the caller. Note that the VFS does not call
1267 * unlink on negative dentries currently.
1268 */
1269 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1270 {
1271 int rc = 0;
1272 unsigned int xid;
1273 char *full_path = NULL;
1274 struct inode *inode = d_inode(dentry);
1275 struct cifsInodeInfo *cifs_inode;
1276 struct super_block *sb = dir->i_sb;
1277 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1278 struct tcon_link *tlink;
1279 struct cifs_tcon *tcon;
1280 struct TCP_Server_Info *server;
1281 struct iattr *attrs = NULL;
1282 __u32 dosattr = 0, origattr = 0;
1283
1284 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1285
1286 tlink = cifs_sb_tlink(cifs_sb);
1287 if (IS_ERR(tlink))
1288 return PTR_ERR(tlink);
1289 tcon = tlink_tcon(tlink);
1290 server = tcon->ses->server;
1291
1292 xid = get_xid();
1293
1294 /* Unlink can be called from rename so we can not take the
1295 * sb->s_vfs_rename_mutex here */
1296 full_path = build_path_from_dentry(dentry);
1297 if (full_path == NULL) {
1298 rc = -ENOMEM;
1299 goto unlink_out;
1300 }
1301
1302 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1303 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1304 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1305 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1306 cifs_remap(cifs_sb));
1307 cifs_dbg(FYI, "posix del rc %d\n", rc);
1308 if ((rc == 0) || (rc == -ENOENT))
1309 goto psx_del_no_retry;
1310 }
1311
1312 retry_std_delete:
1313 if (!server->ops->unlink) {
1314 rc = -ENOSYS;
1315 goto psx_del_no_retry;
1316 }
1317
1318 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1319
1320 psx_del_no_retry:
1321 if (!rc) {
1322 if (inode)
1323 cifs_drop_nlink(inode);
1324 } else if (rc == -ENOENT) {
1325 d_drop(dentry);
1326 } else if (rc == -EBUSY) {
1327 if (server->ops->rename_pending_delete) {
1328 rc = server->ops->rename_pending_delete(full_path,
1329 dentry, xid);
1330 if (rc == 0)
1331 cifs_drop_nlink(inode);
1332 }
1333 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1334 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1335 if (attrs == NULL) {
1336 rc = -ENOMEM;
1337 goto out_reval;
1338 }
1339
1340 /* try to reset dos attributes */
1341 cifs_inode = CIFS_I(inode);
1342 origattr = cifs_inode->cifsAttrs;
1343 if (origattr == 0)
1344 origattr |= ATTR_NORMAL;
1345 dosattr = origattr & ~ATTR_READONLY;
1346 if (dosattr == 0)
1347 dosattr |= ATTR_NORMAL;
1348 dosattr |= ATTR_HIDDEN;
1349
1350 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1351 if (rc != 0)
1352 goto out_reval;
1353
1354 goto retry_std_delete;
1355 }
1356
1357 /* undo the setattr if we errored out and it's needed */
1358 if (rc != 0 && dosattr != 0)
1359 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1360
1361 out_reval:
1362 if (inode) {
1363 cifs_inode = CIFS_I(inode);
1364 cifs_inode->time = 0; /* will force revalidate to get info
1365 when needed */
1366 inode->i_ctime = current_fs_time(sb);
1367 }
1368 dir->i_ctime = dir->i_mtime = current_fs_time(sb);
1369 cifs_inode = CIFS_I(dir);
1370 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1371 unlink_out:
1372 kfree(full_path);
1373 kfree(attrs);
1374 free_xid(xid);
1375 cifs_put_tlink(tlink);
1376 return rc;
1377 }
1378
1379 static int
1380 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1381 const char *full_path, struct cifs_sb_info *cifs_sb,
1382 struct cifs_tcon *tcon, const unsigned int xid)
1383 {
1384 int rc = 0;
1385 struct inode *inode = NULL;
1386
1387 if (tcon->unix_ext)
1388 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1389 xid);
1390 else
1391 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1392 xid, NULL);
1393
1394 if (rc)
1395 return rc;
1396
1397 /*
1398 * setting nlink not necessary except in cases where we failed to get it
1399 * from the server or was set bogus. Also, since this is a brand new
1400 * inode, no need to grab the i_lock before setting the i_nlink.
1401 */
1402 if (inode->i_nlink < 2)
1403 set_nlink(inode, 2);
1404 mode &= ~current_umask();
1405 /* must turn on setgid bit if parent dir has it */
1406 if (parent->i_mode & S_ISGID)
1407 mode |= S_ISGID;
1408
1409 if (tcon->unix_ext) {
1410 struct cifs_unix_set_info_args args = {
1411 .mode = mode,
1412 .ctime = NO_CHANGE_64,
1413 .atime = NO_CHANGE_64,
1414 .mtime = NO_CHANGE_64,
1415 .device = 0,
1416 };
1417 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1418 args.uid = current_fsuid();
1419 if (parent->i_mode & S_ISGID)
1420 args.gid = parent->i_gid;
1421 else
1422 args.gid = current_fsgid();
1423 } else {
1424 args.uid = INVALID_UID; /* no change */
1425 args.gid = INVALID_GID; /* no change */
1426 }
1427 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1428 cifs_sb->local_nls,
1429 cifs_remap(cifs_sb));
1430 } else {
1431 struct TCP_Server_Info *server = tcon->ses->server;
1432 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1433 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1434 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1435 tcon, xid);
1436 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1437 inode->i_mode = (mode | S_IFDIR);
1438
1439 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1440 inode->i_uid = current_fsuid();
1441 if (inode->i_mode & S_ISGID)
1442 inode->i_gid = parent->i_gid;
1443 else
1444 inode->i_gid = current_fsgid();
1445 }
1446 }
1447 d_instantiate(dentry, inode);
1448 return rc;
1449 }
1450
1451 static int
1452 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1453 const char *full_path, struct cifs_sb_info *cifs_sb,
1454 struct cifs_tcon *tcon, const unsigned int xid)
1455 {
1456 int rc = 0;
1457 u32 oplock = 0;
1458 FILE_UNIX_BASIC_INFO *info = NULL;
1459 struct inode *newinode = NULL;
1460 struct cifs_fattr fattr;
1461
1462 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1463 if (info == NULL) {
1464 rc = -ENOMEM;
1465 goto posix_mkdir_out;
1466 }
1467
1468 mode &= ~current_umask();
1469 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1470 NULL /* netfid */, info, &oplock, full_path,
1471 cifs_sb->local_nls, cifs_remap(cifs_sb));
1472 if (rc == -EOPNOTSUPP)
1473 goto posix_mkdir_out;
1474 else if (rc) {
1475 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1476 d_drop(dentry);
1477 goto posix_mkdir_out;
1478 }
1479
1480 if (info->Type == cpu_to_le32(-1))
1481 /* no return info, go query for it */
1482 goto posix_mkdir_get_info;
1483 /*
1484 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1485 * need to set uid/gid.
1486 */
1487
1488 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1489 cifs_fill_uniqueid(inode->i_sb, &fattr);
1490 newinode = cifs_iget(inode->i_sb, &fattr);
1491 if (!newinode)
1492 goto posix_mkdir_get_info;
1493
1494 d_instantiate(dentry, newinode);
1495
1496 #ifdef CONFIG_CIFS_DEBUG2
1497 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1498 dentry, dentry, newinode);
1499
1500 if (newinode->i_nlink != 2)
1501 cifs_dbg(FYI, "unexpected number of links %d\n",
1502 newinode->i_nlink);
1503 #endif
1504
1505 posix_mkdir_out:
1506 kfree(info);
1507 return rc;
1508 posix_mkdir_get_info:
1509 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1510 xid);
1511 goto posix_mkdir_out;
1512 }
1513
1514 int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
1515 {
1516 int rc = 0;
1517 unsigned int xid;
1518 struct cifs_sb_info *cifs_sb;
1519 struct tcon_link *tlink;
1520 struct cifs_tcon *tcon;
1521 struct TCP_Server_Info *server;
1522 char *full_path;
1523
1524 cifs_dbg(FYI, "In cifs_mkdir, mode = 0x%hx inode = 0x%p\n",
1525 mode, inode);
1526
1527 cifs_sb = CIFS_SB(inode->i_sb);
1528 tlink = cifs_sb_tlink(cifs_sb);
1529 if (IS_ERR(tlink))
1530 return PTR_ERR(tlink);
1531 tcon = tlink_tcon(tlink);
1532
1533 xid = get_xid();
1534
1535 full_path = build_path_from_dentry(direntry);
1536 if (full_path == NULL) {
1537 rc = -ENOMEM;
1538 goto mkdir_out;
1539 }
1540
1541 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1542 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1543 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1544 tcon, xid);
1545 if (rc != -EOPNOTSUPP)
1546 goto mkdir_out;
1547 }
1548
1549 server = tcon->ses->server;
1550
1551 if (!server->ops->mkdir) {
1552 rc = -ENOSYS;
1553 goto mkdir_out;
1554 }
1555
1556 /* BB add setting the equivalent of mode via CreateX w/ACLs */
1557 rc = server->ops->mkdir(xid, tcon, full_path, cifs_sb);
1558 if (rc) {
1559 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
1560 d_drop(direntry);
1561 goto mkdir_out;
1562 }
1563
1564 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1565 xid);
1566 mkdir_out:
1567 /*
1568 * Force revalidate to get parent dir info when needed since cached
1569 * attributes are invalid now.
1570 */
1571 CIFS_I(inode)->time = 0;
1572 kfree(full_path);
1573 free_xid(xid);
1574 cifs_put_tlink(tlink);
1575 return rc;
1576 }
1577
1578 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1579 {
1580 int rc = 0;
1581 unsigned int xid;
1582 struct cifs_sb_info *cifs_sb;
1583 struct tcon_link *tlink;
1584 struct cifs_tcon *tcon;
1585 struct TCP_Server_Info *server;
1586 char *full_path = NULL;
1587 struct cifsInodeInfo *cifsInode;
1588
1589 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
1590
1591 xid = get_xid();
1592
1593 full_path = build_path_from_dentry(direntry);
1594 if (full_path == NULL) {
1595 rc = -ENOMEM;
1596 goto rmdir_exit;
1597 }
1598
1599 cifs_sb = CIFS_SB(inode->i_sb);
1600 tlink = cifs_sb_tlink(cifs_sb);
1601 if (IS_ERR(tlink)) {
1602 rc = PTR_ERR(tlink);
1603 goto rmdir_exit;
1604 }
1605 tcon = tlink_tcon(tlink);
1606 server = tcon->ses->server;
1607
1608 if (!server->ops->rmdir) {
1609 rc = -ENOSYS;
1610 cifs_put_tlink(tlink);
1611 goto rmdir_exit;
1612 }
1613
1614 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
1615 cifs_put_tlink(tlink);
1616
1617 if (!rc) {
1618 spin_lock(&d_inode(direntry)->i_lock);
1619 i_size_write(d_inode(direntry), 0);
1620 clear_nlink(d_inode(direntry));
1621 spin_unlock(&d_inode(direntry)->i_lock);
1622 }
1623
1624 cifsInode = CIFS_I(d_inode(direntry));
1625 /* force revalidate to go get info when needed */
1626 cifsInode->time = 0;
1627
1628 cifsInode = CIFS_I(inode);
1629 /*
1630 * Force revalidate to get parent dir info when needed since cached
1631 * attributes are invalid now.
1632 */
1633 cifsInode->time = 0;
1634
1635 d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
1636 current_fs_time(inode->i_sb);
1637
1638 rmdir_exit:
1639 kfree(full_path);
1640 free_xid(xid);
1641 return rc;
1642 }
1643
1644 static int
1645 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
1646 const char *from_path, struct dentry *to_dentry,
1647 const char *to_path)
1648 {
1649 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
1650 struct tcon_link *tlink;
1651 struct cifs_tcon *tcon;
1652 struct TCP_Server_Info *server;
1653 struct cifs_fid fid;
1654 struct cifs_open_parms oparms;
1655 int oplock, rc;
1656
1657 tlink = cifs_sb_tlink(cifs_sb);
1658 if (IS_ERR(tlink))
1659 return PTR_ERR(tlink);
1660 tcon = tlink_tcon(tlink);
1661 server = tcon->ses->server;
1662
1663 if (!server->ops->rename)
1664 return -ENOSYS;
1665
1666 /* try path-based rename first */
1667 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
1668
1669 /*
1670 * Don't bother with rename by filehandle unless file is busy and
1671 * source. Note that cross directory moves do not work with
1672 * rename by filehandle to various Windows servers.
1673 */
1674 if (rc == 0 || rc != -EBUSY)
1675 goto do_rename_exit;
1676
1677 /* open-file renames don't work across directories */
1678 if (to_dentry->d_parent != from_dentry->d_parent)
1679 goto do_rename_exit;
1680
1681 oparms.tcon = tcon;
1682 oparms.cifs_sb = cifs_sb;
1683 /* open the file to be renamed -- we need DELETE perms */
1684 oparms.desired_access = DELETE;
1685 oparms.create_options = CREATE_NOT_DIR;
1686 oparms.disposition = FILE_OPEN;
1687 oparms.path = from_path;
1688 oparms.fid = &fid;
1689 oparms.reconnect = false;
1690
1691 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1692 if (rc == 0) {
1693 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
1694 (const char *) to_dentry->d_name.name,
1695 cifs_sb->local_nls, cifs_remap(cifs_sb));
1696 CIFSSMBClose(xid, tcon, fid.netfid);
1697 }
1698 do_rename_exit:
1699 cifs_put_tlink(tlink);
1700 return rc;
1701 }
1702
1703 int
1704 cifs_rename2(struct inode *source_dir, struct dentry *source_dentry,
1705 struct inode *target_dir, struct dentry *target_dentry,
1706 unsigned int flags)
1707 {
1708 char *from_name = NULL;
1709 char *to_name = NULL;
1710 struct cifs_sb_info *cifs_sb;
1711 struct tcon_link *tlink;
1712 struct cifs_tcon *tcon;
1713 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
1714 FILE_UNIX_BASIC_INFO *info_buf_target;
1715 unsigned int xid;
1716 int rc, tmprc;
1717
1718 if (flags & ~RENAME_NOREPLACE)
1719 return -EINVAL;
1720
1721 cifs_sb = CIFS_SB(source_dir->i_sb);
1722 tlink = cifs_sb_tlink(cifs_sb);
1723 if (IS_ERR(tlink))
1724 return PTR_ERR(tlink);
1725 tcon = tlink_tcon(tlink);
1726
1727 xid = get_xid();
1728
1729 /*
1730 * we already have the rename sem so we do not need to
1731 * grab it again here to protect the path integrity
1732 */
1733 from_name = build_path_from_dentry(source_dentry);
1734 if (from_name == NULL) {
1735 rc = -ENOMEM;
1736 goto cifs_rename_exit;
1737 }
1738
1739 to_name = build_path_from_dentry(target_dentry);
1740 if (to_name == NULL) {
1741 rc = -ENOMEM;
1742 goto cifs_rename_exit;
1743 }
1744
1745 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
1746 to_name);
1747
1748 /*
1749 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
1750 */
1751 if (flags & RENAME_NOREPLACE)
1752 goto cifs_rename_exit;
1753
1754 if (rc == -EEXIST && tcon->unix_ext) {
1755 /*
1756 * Are src and dst hardlinks of same inode? We can only tell
1757 * with unix extensions enabled.
1758 */
1759 info_buf_source =
1760 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
1761 GFP_KERNEL);
1762 if (info_buf_source == NULL) {
1763 rc = -ENOMEM;
1764 goto cifs_rename_exit;
1765 }
1766
1767 info_buf_target = info_buf_source + 1;
1768 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
1769 info_buf_source,
1770 cifs_sb->local_nls,
1771 cifs_remap(cifs_sb));
1772 if (tmprc != 0)
1773 goto unlink_target;
1774
1775 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
1776 info_buf_target,
1777 cifs_sb->local_nls,
1778 cifs_remap(cifs_sb));
1779
1780 if (tmprc == 0 && (info_buf_source->UniqueId ==
1781 info_buf_target->UniqueId)) {
1782 /* same file, POSIX says that this is a noop */
1783 rc = 0;
1784 goto cifs_rename_exit;
1785 }
1786 }
1787 /*
1788 * else ... BB we could add the same check for Windows by
1789 * checking the UniqueId via FILE_INTERNAL_INFO
1790 */
1791
1792 unlink_target:
1793 /* Try unlinking the target dentry if it's not negative */
1794 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
1795 if (d_is_dir(target_dentry))
1796 tmprc = cifs_rmdir(target_dir, target_dentry);
1797 else
1798 tmprc = cifs_unlink(target_dir, target_dentry);
1799 if (tmprc)
1800 goto cifs_rename_exit;
1801 rc = cifs_do_rename(xid, source_dentry, from_name,
1802 target_dentry, to_name);
1803 }
1804
1805 /* force revalidate to go get info when needed */
1806 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
1807
1808 source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
1809 target_dir->i_mtime = current_fs_time(source_dir->i_sb);
1810
1811 cifs_rename_exit:
1812 kfree(info_buf_source);
1813 kfree(from_name);
1814 kfree(to_name);
1815 free_xid(xid);
1816 cifs_put_tlink(tlink);
1817 return rc;
1818 }
1819
1820 static bool
1821 cifs_inode_needs_reval(struct inode *inode)
1822 {
1823 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
1824 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1825
1826 if (CIFS_CACHE_READ(cifs_i))
1827 return false;
1828
1829 if (!lookupCacheEnabled)
1830 return true;
1831
1832 if (cifs_i->time == 0)
1833 return true;
1834
1835 if (!cifs_sb->actimeo)
1836 return true;
1837
1838 if (!time_in_range(jiffies, cifs_i->time,
1839 cifs_i->time + cifs_sb->actimeo))
1840 return true;
1841
1842 /* hardlinked files w/ noserverino get "special" treatment */
1843 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
1844 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
1845 return true;
1846
1847 return false;
1848 }
1849
1850 /*
1851 * Zap the cache. Called when invalid_mapping flag is set.
1852 */
1853 int
1854 cifs_invalidate_mapping(struct inode *inode)
1855 {
1856 int rc = 0;
1857
1858 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
1859 rc = invalidate_inode_pages2(inode->i_mapping);
1860 if (rc)
1861 cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
1862 __func__, inode);
1863 }
1864
1865 cifs_fscache_reset_inode_cookie(inode);
1866 return rc;
1867 }
1868
1869 /**
1870 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
1871 * @word: long word containing the bit lock
1872 */
1873 static int
1874 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
1875 {
1876 freezable_schedule_unsafe();
1877 if (signal_pending_state(mode, current))
1878 return -ERESTARTSYS;
1879 return 0;
1880 }
1881
1882 int
1883 cifs_revalidate_mapping(struct inode *inode)
1884 {
1885 int rc;
1886 unsigned long *flags = &CIFS_I(inode)->flags;
1887
1888 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
1889 TASK_KILLABLE);
1890 if (rc)
1891 return rc;
1892
1893 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
1894 rc = cifs_invalidate_mapping(inode);
1895 if (rc)
1896 set_bit(CIFS_INO_INVALID_MAPPING, flags);
1897 }
1898
1899 clear_bit_unlock(CIFS_INO_LOCK, flags);
1900 smp_mb__after_atomic();
1901 wake_up_bit(flags, CIFS_INO_LOCK);
1902
1903 return rc;
1904 }
1905
1906 int
1907 cifs_zap_mapping(struct inode *inode)
1908 {
1909 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
1910 return cifs_revalidate_mapping(inode);
1911 }
1912
1913 int cifs_revalidate_file_attr(struct file *filp)
1914 {
1915 int rc = 0;
1916 struct inode *inode = file_inode(filp);
1917 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
1918
1919 if (!cifs_inode_needs_reval(inode))
1920 return rc;
1921
1922 if (tlink_tcon(cfile->tlink)->unix_ext)
1923 rc = cifs_get_file_info_unix(filp);
1924 else
1925 rc = cifs_get_file_info(filp);
1926
1927 return rc;
1928 }
1929
1930 int cifs_revalidate_dentry_attr(struct dentry *dentry)
1931 {
1932 unsigned int xid;
1933 int rc = 0;
1934 struct inode *inode = d_inode(dentry);
1935 struct super_block *sb = dentry->d_sb;
1936 char *full_path = NULL;
1937
1938 if (inode == NULL)
1939 return -ENOENT;
1940
1941 if (!cifs_inode_needs_reval(inode))
1942 return rc;
1943
1944 xid = get_xid();
1945
1946 /* can not safely grab the rename sem here if rename calls revalidate
1947 since that would deadlock */
1948 full_path = build_path_from_dentry(dentry);
1949 if (full_path == NULL) {
1950 rc = -ENOMEM;
1951 goto out;
1952 }
1953
1954 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
1955 full_path, inode, inode->i_count.counter,
1956 dentry, cifs_get_time(dentry), jiffies);
1957
1958 if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
1959 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
1960 else
1961 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
1962 xid, NULL);
1963
1964 out:
1965 kfree(full_path);
1966 free_xid(xid);
1967 return rc;
1968 }
1969
1970 int cifs_revalidate_file(struct file *filp)
1971 {
1972 int rc;
1973 struct inode *inode = file_inode(filp);
1974
1975 rc = cifs_revalidate_file_attr(filp);
1976 if (rc)
1977 return rc;
1978
1979 return cifs_revalidate_mapping(inode);
1980 }
1981
1982 /* revalidate a dentry's inode attributes */
1983 int cifs_revalidate_dentry(struct dentry *dentry)
1984 {
1985 int rc;
1986 struct inode *inode = d_inode(dentry);
1987
1988 rc = cifs_revalidate_dentry_attr(dentry);
1989 if (rc)
1990 return rc;
1991
1992 return cifs_revalidate_mapping(inode);
1993 }
1994
1995 int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1996 struct kstat *stat)
1997 {
1998 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
1999 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2000 struct inode *inode = d_inode(dentry);
2001 int rc;
2002
2003 /*
2004 * We need to be sure that all dirty pages are written and the server
2005 * has actual ctime, mtime and file length.
2006 */
2007 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2008 inode->i_mapping->nrpages != 0) {
2009 rc = filemap_fdatawait(inode->i_mapping);
2010 if (rc) {
2011 mapping_set_error(inode->i_mapping, rc);
2012 return rc;
2013 }
2014 }
2015
2016 rc = cifs_revalidate_dentry_attr(dentry);
2017 if (rc)
2018 return rc;
2019
2020 generic_fillattr(inode, stat);
2021 stat->blksize = CIFS_MAX_MSGSIZE;
2022 stat->ino = CIFS_I(inode)->uniqueid;
2023
2024 /*
2025 * If on a multiuser mount without unix extensions or cifsacl being
2026 * enabled, and the admin hasn't overridden them, set the ownership
2027 * to the fsuid/fsgid of the current process.
2028 */
2029 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2030 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2031 !tcon->unix_ext) {
2032 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2033 stat->uid = current_fsuid();
2034 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2035 stat->gid = current_fsgid();
2036 }
2037 return rc;
2038 }
2039
2040 static int cifs_truncate_page(struct address_space *mapping, loff_t from)
2041 {
2042 pgoff_t index = from >> PAGE_SHIFT;
2043 unsigned offset = from & (PAGE_SIZE - 1);
2044 struct page *page;
2045 int rc = 0;
2046
2047 page = grab_cache_page(mapping, index);
2048 if (!page)
2049 return -ENOMEM;
2050
2051 zero_user_segment(page, offset, PAGE_SIZE);
2052 unlock_page(page);
2053 put_page(page);
2054 return rc;
2055 }
2056
2057 static void cifs_setsize(struct inode *inode, loff_t offset)
2058 {
2059 spin_lock(&inode->i_lock);
2060 i_size_write(inode, offset);
2061 spin_unlock(&inode->i_lock);
2062
2063 truncate_pagecache(inode, offset);
2064 }
2065
2066 static int
2067 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2068 unsigned int xid, char *full_path)
2069 {
2070 int rc;
2071 struct cifsFileInfo *open_file;
2072 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2073 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2074 struct tcon_link *tlink = NULL;
2075 struct cifs_tcon *tcon = NULL;
2076 struct TCP_Server_Info *server;
2077
2078 /*
2079 * To avoid spurious oplock breaks from server, in the case of
2080 * inodes that we already have open, avoid doing path based
2081 * setting of file size if we can do it by handle.
2082 * This keeps our caching token (oplock) and avoids timeouts
2083 * when the local oplock break takes longer to flush
2084 * writebehind data than the SMB timeout for the SetPathInfo
2085 * request would allow
2086 */
2087 open_file = find_writable_file(cifsInode, true);
2088 if (open_file) {
2089 tcon = tlink_tcon(open_file->tlink);
2090 server = tcon->ses->server;
2091 if (server->ops->set_file_size)
2092 rc = server->ops->set_file_size(xid, tcon, open_file,
2093 attrs->ia_size, false);
2094 else
2095 rc = -ENOSYS;
2096 cifsFileInfo_put(open_file);
2097 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2098 } else
2099 rc = -EINVAL;
2100
2101 if (!rc)
2102 goto set_size_out;
2103
2104 if (tcon == NULL) {
2105 tlink = cifs_sb_tlink(cifs_sb);
2106 if (IS_ERR(tlink))
2107 return PTR_ERR(tlink);
2108 tcon = tlink_tcon(tlink);
2109 server = tcon->ses->server;
2110 }
2111
2112 /*
2113 * Set file size by pathname rather than by handle either because no
2114 * valid, writeable file handle for it was found or because there was
2115 * an error setting it by handle.
2116 */
2117 if (server->ops->set_path_size)
2118 rc = server->ops->set_path_size(xid, tcon, full_path,
2119 attrs->ia_size, cifs_sb, false);
2120 else
2121 rc = -ENOSYS;
2122 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2123
2124 if (tlink)
2125 cifs_put_tlink(tlink);
2126
2127 set_size_out:
2128 if (rc == 0) {
2129 cifsInode->server_eof = attrs->ia_size;
2130 cifs_setsize(inode, attrs->ia_size);
2131 cifs_truncate_page(inode->i_mapping, inode->i_size);
2132 }
2133
2134 return rc;
2135 }
2136
2137 static int
2138 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2139 {
2140 int rc;
2141 unsigned int xid;
2142 char *full_path = NULL;
2143 struct inode *inode = d_inode(direntry);
2144 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2145 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2146 struct tcon_link *tlink;
2147 struct cifs_tcon *pTcon;
2148 struct cifs_unix_set_info_args *args = NULL;
2149 struct cifsFileInfo *open_file;
2150
2151 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2152 direntry, attrs->ia_valid);
2153
2154 xid = get_xid();
2155
2156 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2157 attrs->ia_valid |= ATTR_FORCE;
2158
2159 rc = setattr_prepare(direntry, attrs);
2160 if (rc < 0)
2161 goto out;
2162
2163 full_path = build_path_from_dentry(direntry);
2164 if (full_path == NULL) {
2165 rc = -ENOMEM;
2166 goto out;
2167 }
2168
2169 /*
2170 * Attempt to flush data before changing attributes. We need to do
2171 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2172 * ownership or mode then we may also need to do this. Here, we take
2173 * the safe way out and just do the flush on all setattr requests. If
2174 * the flush returns error, store it to report later and continue.
2175 *
2176 * BB: This should be smarter. Why bother flushing pages that
2177 * will be truncated anyway? Also, should we error out here if
2178 * the flush returns error?
2179 */
2180 rc = filemap_write_and_wait(inode->i_mapping);
2181 mapping_set_error(inode->i_mapping, rc);
2182 rc = 0;
2183
2184 if (attrs->ia_valid & ATTR_SIZE) {
2185 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2186 if (rc != 0)
2187 goto out;
2188 }
2189
2190 /* skip mode change if it's just for clearing setuid/setgid */
2191 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2192 attrs->ia_valid &= ~ATTR_MODE;
2193
2194 args = kmalloc(sizeof(*args), GFP_KERNEL);
2195 if (args == NULL) {
2196 rc = -ENOMEM;
2197 goto out;
2198 }
2199
2200 /* set up the struct */
2201 if (attrs->ia_valid & ATTR_MODE)
2202 args->mode = attrs->ia_mode;
2203 else
2204 args->mode = NO_CHANGE_64;
2205
2206 if (attrs->ia_valid & ATTR_UID)
2207 args->uid = attrs->ia_uid;
2208 else
2209 args->uid = INVALID_UID; /* no change */
2210
2211 if (attrs->ia_valid & ATTR_GID)
2212 args->gid = attrs->ia_gid;
2213 else
2214 args->gid = INVALID_GID; /* no change */
2215
2216 if (attrs->ia_valid & ATTR_ATIME)
2217 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2218 else
2219 args->atime = NO_CHANGE_64;
2220
2221 if (attrs->ia_valid & ATTR_MTIME)
2222 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2223 else
2224 args->mtime = NO_CHANGE_64;
2225
2226 if (attrs->ia_valid & ATTR_CTIME)
2227 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2228 else
2229 args->ctime = NO_CHANGE_64;
2230
2231 args->device = 0;
2232 open_file = find_writable_file(cifsInode, true);
2233 if (open_file) {
2234 u16 nfid = open_file->fid.netfid;
2235 u32 npid = open_file->pid;
2236 pTcon = tlink_tcon(open_file->tlink);
2237 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2238 cifsFileInfo_put(open_file);
2239 } else {
2240 tlink = cifs_sb_tlink(cifs_sb);
2241 if (IS_ERR(tlink)) {
2242 rc = PTR_ERR(tlink);
2243 goto out;
2244 }
2245 pTcon = tlink_tcon(tlink);
2246 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2247 cifs_sb->local_nls,
2248 cifs_remap(cifs_sb));
2249 cifs_put_tlink(tlink);
2250 }
2251
2252 if (rc)
2253 goto out;
2254
2255 if ((attrs->ia_valid & ATTR_SIZE) &&
2256 attrs->ia_size != i_size_read(inode))
2257 truncate_setsize(inode, attrs->ia_size);
2258
2259 setattr_copy(inode, attrs);
2260 mark_inode_dirty(inode);
2261
2262 /* force revalidate when any of these times are set since some
2263 of the fs types (eg ext3, fat) do not have fine enough
2264 time granularity to match protocol, and we do not have a
2265 a way (yet) to query the server fs's time granularity (and
2266 whether it rounds times down).
2267 */
2268 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2269 cifsInode->time = 0;
2270 out:
2271 kfree(args);
2272 kfree(full_path);
2273 free_xid(xid);
2274 return rc;
2275 }
2276
2277 static int
2278 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2279 {
2280 unsigned int xid;
2281 kuid_t uid = INVALID_UID;
2282 kgid_t gid = INVALID_GID;
2283 struct inode *inode = d_inode(direntry);
2284 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2285 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2286 char *full_path = NULL;
2287 int rc = -EACCES;
2288 __u32 dosattr = 0;
2289 __u64 mode = NO_CHANGE_64;
2290
2291 xid = get_xid();
2292
2293 cifs_dbg(FYI, "setattr on file %pd attrs->iavalid 0x%x\n",
2294 direntry, attrs->ia_valid);
2295
2296 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2297 attrs->ia_valid |= ATTR_FORCE;
2298
2299 rc = setattr_prepare(direntry, attrs);
2300 if (rc < 0) {
2301 free_xid(xid);
2302 return rc;
2303 }
2304
2305 full_path = build_path_from_dentry(direntry);
2306 if (full_path == NULL) {
2307 rc = -ENOMEM;
2308 free_xid(xid);
2309 return rc;
2310 }
2311
2312 /*
2313 * Attempt to flush data before changing attributes. We need to do
2314 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2315 * ownership or mode then we may also need to do this. Here, we take
2316 * the safe way out and just do the flush on all setattr requests. If
2317 * the flush returns error, store it to report later and continue.
2318 *
2319 * BB: This should be smarter. Why bother flushing pages that
2320 * will be truncated anyway? Also, should we error out here if
2321 * the flush returns error?
2322 */
2323 rc = filemap_write_and_wait(inode->i_mapping);
2324 mapping_set_error(inode->i_mapping, rc);
2325 rc = 0;
2326
2327 if (attrs->ia_valid & ATTR_SIZE) {
2328 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2329 if (rc != 0)
2330 goto cifs_setattr_exit;
2331 }
2332
2333 if (attrs->ia_valid & ATTR_UID)
2334 uid = attrs->ia_uid;
2335
2336 if (attrs->ia_valid & ATTR_GID)
2337 gid = attrs->ia_gid;
2338
2339 #ifdef CONFIG_CIFS_ACL
2340 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
2341 if (uid_valid(uid) || gid_valid(gid)) {
2342 rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
2343 uid, gid);
2344 if (rc) {
2345 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2346 __func__, rc);
2347 goto cifs_setattr_exit;
2348 }
2349 }
2350 } else
2351 #endif /* CONFIG_CIFS_ACL */
2352 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
2353 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
2354
2355 /* skip mode change if it's just for clearing setuid/setgid */
2356 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2357 attrs->ia_valid &= ~ATTR_MODE;
2358
2359 if (attrs->ia_valid & ATTR_MODE) {
2360 mode = attrs->ia_mode;
2361 rc = 0;
2362 #ifdef CONFIG_CIFS_ACL
2363 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
2364 rc = id_mode_to_cifs_acl(inode, full_path, mode,
2365 INVALID_UID, INVALID_GID);
2366 if (rc) {
2367 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2368 __func__, rc);
2369 goto cifs_setattr_exit;
2370 }
2371 } else
2372 #endif /* CONFIG_CIFS_ACL */
2373 if (((mode & S_IWUGO) == 0) &&
2374 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
2375
2376 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2377
2378 /* fix up mode if we're not using dynperm */
2379 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2380 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2381 } else if ((mode & S_IWUGO) &&
2382 (cifsInode->cifsAttrs & ATTR_READONLY)) {
2383
2384 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2385 /* Attributes of 0 are ignored */
2386 if (dosattr == 0)
2387 dosattr |= ATTR_NORMAL;
2388
2389 /* reset local inode permissions to normal */
2390 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2391 attrs->ia_mode &= ~(S_IALLUGO);
2392 if (S_ISDIR(inode->i_mode))
2393 attrs->ia_mode |=
2394 cifs_sb->mnt_dir_mode;
2395 else
2396 attrs->ia_mode |=
2397 cifs_sb->mnt_file_mode;
2398 }
2399 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2400 /* ignore mode change - ATTR_READONLY hasn't changed */
2401 attrs->ia_valid &= ~ATTR_MODE;
2402 }
2403 }
2404
2405 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
2406 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
2407 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2408 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
2409
2410 /* Even if error on time set, no sense failing the call if
2411 the server would set the time to a reasonable value anyway,
2412 and this check ensures that we are not being called from
2413 sys_utimes in which case we ought to fail the call back to
2414 the user when the server rejects the call */
2415 if ((rc) && (attrs->ia_valid &
2416 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
2417 rc = 0;
2418 }
2419
2420 /* do not need local check to inode_check_ok since the server does
2421 that */
2422 if (rc)
2423 goto cifs_setattr_exit;
2424
2425 if ((attrs->ia_valid & ATTR_SIZE) &&
2426 attrs->ia_size != i_size_read(inode))
2427 truncate_setsize(inode, attrs->ia_size);
2428
2429 setattr_copy(inode, attrs);
2430 mark_inode_dirty(inode);
2431
2432 cifs_setattr_exit:
2433 kfree(full_path);
2434 free_xid(xid);
2435 return rc;
2436 }
2437
2438 int
2439 cifs_setattr(struct dentry *direntry, struct iattr *attrs)
2440 {
2441 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
2442 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
2443
2444 if (pTcon->unix_ext)
2445 return cifs_setattr_unix(direntry, attrs);
2446
2447 return cifs_setattr_nounix(direntry, attrs);
2448
2449 /* BB: add cifs_setattr_legacy for really old servers */
2450 }
2451
2452 #if 0
2453 void cifs_delete_inode(struct inode *inode)
2454 {
2455 cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode);
2456 /* may have to add back in if and when safe distributed caching of
2457 directories added e.g. via FindNotify */
2458 }
2459 #endif