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