1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002,2010
5 * Author(s): Steve French (sfrench@us.ibm.com)
9 #include <linux/stat.h>
10 #include <linux/slab.h>
11 #include <linux/pagemap.h>
12 #include <linux/freezer.h>
13 #include <linux/sched/signal.h>
14 #include <linux/wait_bit.h>
15 #include <linux/fiemap.h>
16 #include <asm/div64.h>
20 #include "cifsproto.h"
21 #include "smb2proto.h"
22 #include "cifs_debug.h"
23 #include "cifs_fs_sb.h"
24 #include "cifs_unicode.h"
26 #include "fs_context.h"
27 #include "cifs_ioctl.h"
28 #include "cached_dir.h"
32 * Set parameters for the netfs library
34 static void cifs_set_netfs_context(struct inode
*inode
)
36 struct cifsInodeInfo
*cifs_i
= CIFS_I(inode
);
38 netfs_inode_init(&cifs_i
->netfs
, &cifs_req_ops
, true);
41 static void cifs_set_ops(struct inode
*inode
)
43 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
44 struct netfs_inode
*ictx
= netfs_inode(inode
);
46 switch (inode
->i_mode
& S_IFMT
) {
48 inode
->i_op
= &cifs_file_inode_ops
;
49 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_DIRECT_IO
) {
50 set_bit(NETFS_ICTX_UNBUFFERED
, &ictx
->flags
);
51 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_BRL
)
52 inode
->i_fop
= &cifs_file_direct_nobrl_ops
;
54 inode
->i_fop
= &cifs_file_direct_ops
;
55 } else if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_STRICT_IO
) {
56 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_BRL
)
57 inode
->i_fop
= &cifs_file_strict_nobrl_ops
;
59 inode
->i_fop
= &cifs_file_strict_ops
;
60 } else if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_BRL
)
61 inode
->i_fop
= &cifs_file_nobrl_ops
;
62 else { /* not direct, send byte range locks */
63 inode
->i_fop
= &cifs_file_ops
;
66 /* check if server can support readahead */
67 if (cifs_sb_master_tcon(cifs_sb
)->ses
->server
->max_read
<
68 PAGE_SIZE
+ MAX_CIFS_HDR_SIZE
)
69 inode
->i_data
.a_ops
= &cifs_addr_ops_smallbuf
;
71 inode
->i_data
.a_ops
= &cifs_addr_ops
;
72 mapping_set_large_folios(inode
->i_mapping
);
75 if (IS_AUTOMOUNT(inode
)) {
76 inode
->i_op
= &cifs_namespace_inode_operations
;
78 inode
->i_op
= &cifs_dir_inode_ops
;
79 inode
->i_fop
= &cifs_dir_ops
;
83 inode
->i_op
= &cifs_symlink_inode_ops
;
86 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
91 /* check inode attributes against fattr. If they don't match, tag the
92 * inode for cache invalidation
95 cifs_revalidate_cache(struct inode
*inode
, struct cifs_fattr
*fattr
)
97 struct cifs_fscache_inode_coherency_data cd
;
98 struct cifsInodeInfo
*cifs_i
= CIFS_I(inode
);
99 struct timespec64 mtime
;
101 cifs_dbg(FYI
, "%s: revalidating inode %llu\n",
102 __func__
, cifs_i
->uniqueid
);
104 if (inode
->i_state
& I_NEW
) {
105 cifs_dbg(FYI
, "%s: inode %llu is new\n",
106 __func__
, cifs_i
->uniqueid
);
110 /* don't bother with revalidation if we have an oplock */
111 if (CIFS_CACHE_READ(cifs_i
)) {
112 cifs_dbg(FYI
, "%s: inode %llu is oplocked\n",
113 __func__
, cifs_i
->uniqueid
);
117 /* revalidate if mtime or size have changed */
118 fattr
->cf_mtime
= timestamp_truncate(fattr
->cf_mtime
, inode
);
119 mtime
= inode_get_mtime(inode
);
120 if (timespec64_equal(&mtime
, &fattr
->cf_mtime
) &&
121 cifs_i
->netfs
.remote_i_size
== fattr
->cf_eof
) {
122 cifs_dbg(FYI
, "%s: inode %llu is unchanged\n",
123 __func__
, cifs_i
->uniqueid
);
127 cifs_dbg(FYI
, "%s: invalidating inode %llu mapping\n",
128 __func__
, cifs_i
->uniqueid
);
129 set_bit(CIFS_INO_INVALID_MAPPING
, &cifs_i
->flags
);
130 /* Invalidate fscache cookie */
131 cifs_fscache_fill_coherency(&cifs_i
->netfs
.inode
, &cd
);
132 fscache_invalidate(cifs_inode_cookie(inode
), &cd
, i_size_read(inode
), 0);
136 * copy nlink to the inode, unless it wasn't provided. Provide
137 * sane values if we don't have an existing one and none was provided
140 cifs_nlink_fattr_to_inode(struct inode
*inode
, struct cifs_fattr
*fattr
)
143 * if we're in a situation where we can't trust what we
144 * got from the server (readdir, some non-unix cases)
145 * fake reasonable values
147 if (fattr
->cf_flags
& CIFS_FATTR_UNKNOWN_NLINK
) {
148 /* only provide fake values on a new inode */
149 if (inode
->i_state
& I_NEW
) {
150 if (fattr
->cf_cifsattrs
& ATTR_DIRECTORY
)
158 /* we trust the server, so update it */
159 set_nlink(inode
, fattr
->cf_nlink
);
162 /* populate an inode with info from a cifs_fattr struct */
164 cifs_fattr_to_inode(struct inode
*inode
, struct cifs_fattr
*fattr
,
167 struct cifsInodeInfo
*cifs_i
= CIFS_I(inode
);
168 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
170 if (!(inode
->i_state
& I_NEW
) &&
171 unlikely(inode_wrong_type(inode
, fattr
->cf_mode
))) {
172 CIFS_I(inode
)->time
= 0; /* force reval */
175 if (inode
->i_state
& I_NEW
)
176 CIFS_I(inode
)->netfs
.zero_point
= fattr
->cf_eof
;
178 cifs_revalidate_cache(inode
, fattr
);
180 spin_lock(&inode
->i_lock
);
181 fattr
->cf_mtime
= timestamp_truncate(fattr
->cf_mtime
, inode
);
182 fattr
->cf_atime
= timestamp_truncate(fattr
->cf_atime
, inode
);
183 fattr
->cf_ctime
= timestamp_truncate(fattr
->cf_ctime
, inode
);
184 /* we do not want atime to be less than mtime, it broke some apps */
185 if (timespec64_compare(&fattr
->cf_atime
, &fattr
->cf_mtime
) < 0)
186 inode_set_atime_to_ts(inode
, fattr
->cf_mtime
);
188 inode_set_atime_to_ts(inode
, fattr
->cf_atime
);
189 inode_set_mtime_to_ts(inode
, fattr
->cf_mtime
);
190 inode_set_ctime_to_ts(inode
, fattr
->cf_ctime
);
191 inode
->i_rdev
= fattr
->cf_rdev
;
192 cifs_nlink_fattr_to_inode(inode
, fattr
);
193 inode
->i_uid
= fattr
->cf_uid
;
194 inode
->i_gid
= fattr
->cf_gid
;
196 /* if dynperm is set, don't clobber existing mode */
197 if (inode
->i_state
& I_NEW
||
198 !(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_DYNPERM
))
199 inode
->i_mode
= fattr
->cf_mode
;
201 cifs_i
->cifsAttrs
= fattr
->cf_cifsattrs
;
202 cifs_i
->reparse_tag
= fattr
->cf_cifstag
;
204 if (fattr
->cf_flags
& CIFS_FATTR_NEED_REVAL
)
207 cifs_i
->time
= jiffies
;
209 if (fattr
->cf_flags
& CIFS_FATTR_DELETE_PENDING
)
210 set_bit(CIFS_INO_DELETE_PENDING
, &cifs_i
->flags
);
212 clear_bit(CIFS_INO_DELETE_PENDING
, &cifs_i
->flags
);
214 cifs_i
->netfs
.remote_i_size
= fattr
->cf_eof
;
216 * Can't safely change the file size here if the client is writing to
217 * it due to potential races.
219 if (is_size_safe_to_change(cifs_i
, fattr
->cf_eof
, from_readdir
)) {
220 i_size_write(inode
, fattr
->cf_eof
);
223 * i_blocks is not related to (i_size / i_blksize),
224 * but instead 512 byte (2**9) size is required for
225 * calculating num blocks.
227 inode
->i_blocks
= (512 - 1 + fattr
->cf_bytes
) >> 9;
230 if (S_ISLNK(fattr
->cf_mode
) && fattr
->cf_symlink_target
) {
231 kfree(cifs_i
->symlink_target
);
232 cifs_i
->symlink_target
= fattr
->cf_symlink_target
;
233 fattr
->cf_symlink_target
= NULL
;
235 spin_unlock(&inode
->i_lock
);
237 if (fattr
->cf_flags
& CIFS_FATTR_JUNCTION
)
238 inode
->i_flags
|= S_AUTOMOUNT
;
239 if (inode
->i_state
& I_NEW
) {
240 cifs_set_netfs_context(inode
);
247 cifs_fill_uniqueid(struct super_block
*sb
, struct cifs_fattr
*fattr
)
249 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
251 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
)
254 fattr
->cf_uniqueid
= iunique(sb
, ROOT_I
);
257 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
259 cifs_unix_basic_to_fattr(struct cifs_fattr
*fattr
, FILE_UNIX_BASIC_INFO
*info
,
260 struct cifs_sb_info
*cifs_sb
)
262 memset(fattr
, 0, sizeof(*fattr
));
263 fattr
->cf_uniqueid
= le64_to_cpu(info
->UniqueId
);
264 fattr
->cf_bytes
= le64_to_cpu(info
->NumOfBytes
);
265 fattr
->cf_eof
= le64_to_cpu(info
->EndOfFile
);
267 fattr
->cf_atime
= cifs_NTtimeToUnix(info
->LastAccessTime
);
268 fattr
->cf_mtime
= cifs_NTtimeToUnix(info
->LastModificationTime
);
269 fattr
->cf_ctime
= cifs_NTtimeToUnix(info
->LastStatusChange
);
270 /* old POSIX extensions don't get create time */
272 fattr
->cf_mode
= le64_to_cpu(info
->Permissions
);
275 * Since we set the inode type below we need to mask off
276 * to avoid strange results if bits set above.
278 fattr
->cf_mode
&= ~S_IFMT
;
279 switch (le32_to_cpu(info
->Type
)) {
281 fattr
->cf_mode
|= S_IFREG
;
282 fattr
->cf_dtype
= DT_REG
;
285 fattr
->cf_mode
|= S_IFLNK
;
286 fattr
->cf_dtype
= DT_LNK
;
289 fattr
->cf_mode
|= S_IFDIR
;
290 fattr
->cf_dtype
= DT_DIR
;
293 fattr
->cf_mode
|= S_IFCHR
;
294 fattr
->cf_dtype
= DT_CHR
;
295 fattr
->cf_rdev
= MKDEV(le64_to_cpu(info
->DevMajor
),
296 le64_to_cpu(info
->DevMinor
) & MINORMASK
);
299 fattr
->cf_mode
|= S_IFBLK
;
300 fattr
->cf_dtype
= DT_BLK
;
301 fattr
->cf_rdev
= MKDEV(le64_to_cpu(info
->DevMajor
),
302 le64_to_cpu(info
->DevMinor
) & MINORMASK
);
305 fattr
->cf_mode
|= S_IFIFO
;
306 fattr
->cf_dtype
= DT_FIFO
;
309 fattr
->cf_mode
|= S_IFSOCK
;
310 fattr
->cf_dtype
= DT_SOCK
;
313 /* safest to call it a file if we do not know */
314 fattr
->cf_mode
|= S_IFREG
;
315 fattr
->cf_dtype
= DT_REG
;
316 cifs_dbg(FYI
, "unknown type %d\n", le32_to_cpu(info
->Type
));
320 fattr
->cf_uid
= cifs_sb
->ctx
->linux_uid
;
321 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_OVERR_UID
)) {
322 u64 id
= le64_to_cpu(info
->Uid
);
323 if (id
< ((uid_t
)-1)) {
324 kuid_t uid
= make_kuid(&init_user_ns
, id
);
330 fattr
->cf_gid
= cifs_sb
->ctx
->linux_gid
;
331 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_OVERR_GID
)) {
332 u64 id
= le64_to_cpu(info
->Gid
);
333 if (id
< ((gid_t
)-1)) {
334 kgid_t gid
= make_kgid(&init_user_ns
, id
);
340 fattr
->cf_nlink
= le64_to_cpu(info
->Nlinks
);
344 * Fill a cifs_fattr struct with fake inode info.
346 * Needed to setup cifs_fattr data for the directory which is the
347 * junction to the new submount (ie to setup the fake directory
348 * which represents a DFS referral or reparse mount point).
350 static void cifs_create_junction_fattr(struct cifs_fattr
*fattr
,
351 struct super_block
*sb
)
353 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
355 cifs_dbg(FYI
, "%s: creating fake fattr\n", __func__
);
357 memset(fattr
, 0, sizeof(*fattr
));
358 fattr
->cf_mode
= S_IFDIR
| S_IXUGO
| S_IRWXU
;
359 fattr
->cf_uid
= cifs_sb
->ctx
->linux_uid
;
360 fattr
->cf_gid
= cifs_sb
->ctx
->linux_gid
;
361 ktime_get_coarse_real_ts64(&fattr
->cf_mtime
);
362 fattr
->cf_atime
= fattr
->cf_ctime
= fattr
->cf_mtime
;
364 fattr
->cf_flags
= CIFS_FATTR_JUNCTION
;
367 /* Update inode with final fattr data */
368 static int update_inode_info(struct super_block
*sb
,
369 struct cifs_fattr
*fattr
,
370 struct inode
**inode
)
372 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
376 *inode
= cifs_iget(sb
, fattr
);
381 /* We already have inode, update it.
383 * If file type or uniqueid is different, return error.
385 if (unlikely((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
) &&
386 CIFS_I(*inode
)->uniqueid
!= fattr
->cf_uniqueid
)) {
387 CIFS_I(*inode
)->time
= 0; /* force reval */
390 return cifs_fattr_to_inode(*inode
, fattr
, false);
393 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
395 cifs_get_file_info_unix(struct file
*filp
)
399 FILE_UNIX_BASIC_INFO find_data
;
400 struct cifs_fattr fattr
= {};
401 struct inode
*inode
= file_inode(filp
);
402 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
403 struct cifsFileInfo
*cfile
= filp
->private_data
;
404 struct cifs_tcon
*tcon
= tlink_tcon(cfile
->tlink
);
408 if (cfile
->symlink_target
) {
409 fattr
.cf_symlink_target
= kstrdup(cfile
->symlink_target
, GFP_KERNEL
);
410 if (!fattr
.cf_symlink_target
) {
412 goto cifs_gfiunix_out
;
416 rc
= CIFSSMBUnixQFileInfo(xid
, tcon
, cfile
->fid
.netfid
, &find_data
);
418 cifs_unix_basic_to_fattr(&fattr
, &find_data
, cifs_sb
);
419 } else if (rc
== -EREMOTE
) {
420 cifs_create_junction_fattr(&fattr
, inode
->i_sb
);
422 goto cifs_gfiunix_out
;
424 rc
= cifs_fattr_to_inode(inode
, &fattr
, false);
431 static int cifs_get_unix_fattr(const unsigned char *full_path
,
432 struct super_block
*sb
,
433 struct cifs_fattr
*fattr
,
434 struct inode
**pinode
,
435 const unsigned int xid
)
437 struct TCP_Server_Info
*server
;
438 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
439 FILE_UNIX_BASIC_INFO find_data
;
440 struct cifs_tcon
*tcon
;
441 struct tcon_link
*tlink
;
444 cifs_dbg(FYI
, "Getting info on %s\n", full_path
);
446 tlink
= cifs_sb_tlink(cifs_sb
);
448 return PTR_ERR(tlink
);
449 tcon
= tlink_tcon(tlink
);
450 server
= tcon
->ses
->server
;
452 /* could have done a find first instead but this returns more info */
453 rc
= CIFSSMBUnixQPathInfo(xid
, tcon
, full_path
, &find_data
,
454 cifs_sb
->local_nls
, cifs_remap(cifs_sb
));
455 cifs_dbg(FYI
, "%s: query path info: rc = %d\n", __func__
, rc
);
456 cifs_put_tlink(tlink
);
459 cifs_unix_basic_to_fattr(fattr
, &find_data
, cifs_sb
);
460 } else if (rc
== -EREMOTE
) {
461 cifs_create_junction_fattr(fattr
, sb
);
468 cifs_fill_uniqueid(sb
, fattr
);
470 /* check for Minshall+French symlinks */
471 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MF_SYMLINKS
) {
472 tmprc
= check_mf_symlink(xid
, tcon
, cifs_sb
, fattr
, full_path
);
473 cifs_dbg(FYI
, "check_mf_symlink: %d\n", tmprc
);
476 if (S_ISLNK(fattr
->cf_mode
) && !fattr
->cf_symlink_target
) {
477 if (!server
->ops
->query_symlink
)
479 rc
= server
->ops
->query_symlink(xid
, tcon
,
481 &fattr
->cf_symlink_target
);
482 cifs_dbg(FYI
, "%s: query_symlink: %d\n", __func__
, rc
);
487 int cifs_get_inode_info_unix(struct inode
**pinode
,
488 const unsigned char *full_path
,
489 struct super_block
*sb
, unsigned int xid
)
491 struct cifs_fattr fattr
= {};
494 rc
= cifs_get_unix_fattr(full_path
, sb
, &fattr
, pinode
, xid
);
498 rc
= update_inode_info(sb
, &fattr
, pinode
);
500 kfree(fattr
.cf_symlink_target
);
504 static inline int cifs_get_unix_fattr(const unsigned char *full_path
,
505 struct super_block
*sb
,
506 struct cifs_fattr
*fattr
,
507 struct inode
**pinode
,
508 const unsigned int xid
)
513 int cifs_get_inode_info_unix(struct inode
**pinode
,
514 const unsigned char *full_path
,
515 struct super_block
*sb
, unsigned int xid
)
519 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
522 cifs_sfu_type(struct cifs_fattr
*fattr
, const char *path
,
523 struct cifs_sb_info
*cifs_sb
, unsigned int xid
)
527 struct tcon_link
*tlink
;
528 struct cifs_tcon
*tcon
;
530 struct cifs_open_parms oparms
;
531 struct cifs_io_parms io_parms
= {0};
532 char *symlink_buf_utf16
;
533 unsigned int symlink_len_utf16
;
535 unsigned int bytes_read
;
537 int buf_type
= CIFS_NO_BUFFER
;
541 fattr
->cf_mode
&= ~S_IFMT
;
543 if (fattr
->cf_eof
== 0) {
544 cifs_dbg(FYI
, "Fifo\n");
545 fattr
->cf_mode
|= S_IFIFO
;
546 fattr
->cf_dtype
= DT_FIFO
;
548 } else if (fattr
->cf_eof
> 1 && fattr
->cf_eof
< 8) {
549 fattr
->cf_mode
|= S_IFREG
;
550 fattr
->cf_dtype
= DT_REG
;
551 return -EINVAL
; /* EOPNOTSUPP? */
554 tlink
= cifs_sb_tlink(cifs_sb
);
556 return PTR_ERR(tlink
);
557 tcon
= tlink_tcon(tlink
);
559 oparms
= (struct cifs_open_parms
) {
562 .desired_access
= GENERIC_READ
,
563 .create_options
= cifs_create_options(cifs_sb
, CREATE_NOT_DIR
),
564 .disposition
= FILE_OPEN
,
569 if (tcon
->ses
->server
->oplocks
)
573 rc
= tcon
->ses
->server
->ops
->open(xid
, &oparms
, &oplock
, NULL
);
575 cifs_dbg(FYI
, "check sfu type of %s, open rc = %d\n", path
, rc
);
576 cifs_put_tlink(tlink
);
581 io_parms
.netfid
= fid
.netfid
;
582 io_parms
.pid
= current
->tgid
;
583 io_parms
.tcon
= tcon
;
585 io_parms
.length
= 24;
587 rc
= tcon
->ses
->server
->ops
->sync_read(xid
, &fid
, &io_parms
,
588 &bytes_read
, &pbuf
, &buf_type
);
589 if ((rc
== 0) && (bytes_read
>= 8)) {
590 if (memcmp("IntxBLK\0", pbuf
, 8) == 0) {
591 cifs_dbg(FYI
, "Block device\n");
592 fattr
->cf_mode
|= S_IFBLK
;
593 fattr
->cf_dtype
= DT_BLK
;
594 if (bytes_read
== 24) {
595 /* we have enough to decode dev num */
596 __u64 mjr
; /* major */
597 __u64 mnr
; /* minor */
598 mjr
= le64_to_cpu(*(__le64
*)(pbuf
+8));
599 mnr
= le64_to_cpu(*(__le64
*)(pbuf
+16));
600 fattr
->cf_rdev
= MKDEV(mjr
, mnr
);
602 } else if (memcmp("IntxCHR\0", pbuf
, 8) == 0) {
603 cifs_dbg(FYI
, "Char device\n");
604 fattr
->cf_mode
|= S_IFCHR
;
605 fattr
->cf_dtype
= DT_CHR
;
606 if (bytes_read
== 24) {
607 /* we have enough to decode dev num */
608 __u64 mjr
; /* major */
609 __u64 mnr
; /* minor */
610 mjr
= le64_to_cpu(*(__le64
*)(pbuf
+8));
611 mnr
= le64_to_cpu(*(__le64
*)(pbuf
+16));
612 fattr
->cf_rdev
= MKDEV(mjr
, mnr
);
614 } else if (memcmp("LnxSOCK", pbuf
, 8) == 0) {
615 cifs_dbg(FYI
, "Socket\n");
616 fattr
->cf_mode
|= S_IFSOCK
;
617 fattr
->cf_dtype
= DT_SOCK
;
618 } else if (memcmp("IntxLNK\1", pbuf
, 8) == 0) {
619 cifs_dbg(FYI
, "Symlink\n");
620 fattr
->cf_mode
|= S_IFLNK
;
621 fattr
->cf_dtype
= DT_LNK
;
622 if ((fattr
->cf_eof
> 8) && (fattr
->cf_eof
% 2 == 0)) {
623 symlink_buf_utf16
= kmalloc(fattr
->cf_eof
-8 + 1, GFP_KERNEL
);
624 if (symlink_buf_utf16
) {
626 io_parms
.length
= fattr
->cf_eof
-8 + 1;
627 buf_type
= CIFS_NO_BUFFER
;
628 rc
= tcon
->ses
->server
->ops
->sync_read(xid
, &fid
, &io_parms
,
633 (symlink_len_utf16
> 0) &&
634 (symlink_len_utf16
< fattr
->cf_eof
-8 + 1) &&
635 (symlink_len_utf16
% 2 == 0)) {
636 fattr
->cf_symlink_target
=
637 cifs_strndup_from_utf16(symlink_buf_utf16
,
641 if (!fattr
->cf_symlink_target
)
644 kfree(symlink_buf_utf16
);
649 } else if (memcmp("LnxFIFO", pbuf
, 8) == 0) {
650 cifs_dbg(FYI
, "FIFO\n");
651 fattr
->cf_mode
|= S_IFIFO
;
652 fattr
->cf_dtype
= DT_FIFO
;
654 fattr
->cf_mode
|= S_IFREG
; /* file? */
655 fattr
->cf_dtype
= DT_REG
;
658 } else if ((rc
== 0) && (bytes_read
== 1) && (pbuf
[0] == '\0')) {
659 cifs_dbg(FYI
, "Socket\n");
660 fattr
->cf_mode
|= S_IFSOCK
;
661 fattr
->cf_dtype
= DT_SOCK
;
663 fattr
->cf_mode
|= S_IFREG
; /* then it is a file */
664 fattr
->cf_dtype
= DT_REG
;
665 rc
= -EOPNOTSUPP
; /* or some unknown SFU type */
668 tcon
->ses
->server
->ops
->close(xid
, tcon
, &fid
);
669 cifs_put_tlink(tlink
);
673 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
676 * Fetch mode bits as provided by SFU.
678 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
680 static int cifs_sfu_mode(struct cifs_fattr
*fattr
, const unsigned char *path
,
681 struct cifs_sb_info
*cifs_sb
, unsigned int xid
)
683 #ifdef CONFIG_CIFS_XATTR
687 struct tcon_link
*tlink
;
688 struct cifs_tcon
*tcon
;
690 tlink
= cifs_sb_tlink(cifs_sb
);
692 return PTR_ERR(tlink
);
693 tcon
= tlink_tcon(tlink
);
695 if (tcon
->ses
->server
->ops
->query_all_EAs
== NULL
) {
696 cifs_put_tlink(tlink
);
700 rc
= tcon
->ses
->server
->ops
->query_all_EAs(xid
, tcon
, path
,
701 "SETFILEBITS", ea_value
, 4 /* size of buf */,
703 cifs_put_tlink(tlink
);
707 mode
= le32_to_cpu(*((__le32
*)ea_value
));
708 fattr
->cf_mode
&= ~SFBITS_MASK
;
709 cifs_dbg(FYI
, "special bits 0%o org mode 0%o\n",
710 mode
, fattr
->cf_mode
);
711 fattr
->cf_mode
= (mode
& SFBITS_MASK
) | fattr
->cf_mode
;
712 cifs_dbg(FYI
, "special mode bits 0%o\n", mode
);
721 /* Fill a cifs_fattr struct with info from POSIX info struct */
722 static void smb311_posix_info_to_fattr(struct cifs_fattr
*fattr
,
723 struct cifs_open_info_data
*data
,
724 struct super_block
*sb
)
726 struct smb311_posix_qinfo
*info
= &data
->posix_fi
;
727 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
728 struct cifs_tcon
*tcon
= cifs_sb_master_tcon(cifs_sb
);
730 memset(fattr
, 0, sizeof(*fattr
));
732 /* no fattr->flags to set */
733 fattr
->cf_cifsattrs
= le32_to_cpu(info
->DosAttributes
);
734 fattr
->cf_uniqueid
= le64_to_cpu(info
->Inode
);
736 if (info
->LastAccessTime
)
737 fattr
->cf_atime
= cifs_NTtimeToUnix(info
->LastAccessTime
);
739 ktime_get_coarse_real_ts64(&fattr
->cf_atime
);
741 fattr
->cf_ctime
= cifs_NTtimeToUnix(info
->ChangeTime
);
742 fattr
->cf_mtime
= cifs_NTtimeToUnix(info
->LastWriteTime
);
744 if (data
->adjust_tz
) {
745 fattr
->cf_ctime
.tv_sec
+= tcon
->ses
->server
->timeAdj
;
746 fattr
->cf_mtime
.tv_sec
+= tcon
->ses
->server
->timeAdj
;
750 * The srv fs device id is overridden on network mount so setting
751 * @fattr->cf_rdev isn't needed here.
753 fattr
->cf_eof
= le64_to_cpu(info
->EndOfFile
);
754 fattr
->cf_bytes
= le64_to_cpu(info
->AllocationSize
);
755 fattr
->cf_createtime
= le64_to_cpu(info
->CreationTime
);
756 fattr
->cf_nlink
= le32_to_cpu(info
->HardLinks
);
757 fattr
->cf_mode
= (umode_t
) le32_to_cpu(info
->Mode
);
759 if (cifs_open_data_reparse(data
) &&
760 cifs_reparse_point_to_fattr(cifs_sb
, fattr
, data
))
763 fattr
->cf_mode
&= ~S_IFMT
;
764 if (fattr
->cf_cifsattrs
& ATTR_DIRECTORY
) {
765 fattr
->cf_mode
|= S_IFDIR
;
766 fattr
->cf_dtype
= DT_DIR
;
768 fattr
->cf_mode
|= S_IFREG
;
769 fattr
->cf_dtype
= DT_REG
;
773 if (S_ISLNK(fattr
->cf_mode
)) {
774 if (likely(data
->symlink_target
))
775 fattr
->cf_eof
= strnlen(data
->symlink_target
, PATH_MAX
);
776 fattr
->cf_symlink_target
= data
->symlink_target
;
777 data
->symlink_target
= NULL
;
779 sid_to_id(cifs_sb
, &data
->posix_owner
, fattr
, SIDOWNER
);
780 sid_to_id(cifs_sb
, &data
->posix_group
, fattr
, SIDGROUP
);
782 cifs_dbg(FYI
, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
783 fattr
->cf_mode
, fattr
->cf_uniqueid
, fattr
->cf_nlink
);
786 static void cifs_open_info_to_fattr(struct cifs_fattr
*fattr
,
787 struct cifs_open_info_data
*data
,
788 struct super_block
*sb
)
790 struct smb2_file_all_info
*info
= &data
->fi
;
791 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
792 struct cifs_tcon
*tcon
= cifs_sb_master_tcon(cifs_sb
);
794 memset(fattr
, 0, sizeof(*fattr
));
795 fattr
->cf_cifsattrs
= le32_to_cpu(info
->Attributes
);
796 if (info
->DeletePending
)
797 fattr
->cf_flags
|= CIFS_FATTR_DELETE_PENDING
;
799 if (info
->LastAccessTime
)
800 fattr
->cf_atime
= cifs_NTtimeToUnix(info
->LastAccessTime
);
802 ktime_get_coarse_real_ts64(&fattr
->cf_atime
);
804 fattr
->cf_ctime
= cifs_NTtimeToUnix(info
->ChangeTime
);
805 fattr
->cf_mtime
= cifs_NTtimeToUnix(info
->LastWriteTime
);
807 if (data
->adjust_tz
) {
808 fattr
->cf_ctime
.tv_sec
+= tcon
->ses
->server
->timeAdj
;
809 fattr
->cf_mtime
.tv_sec
+= tcon
->ses
->server
->timeAdj
;
812 fattr
->cf_eof
= le64_to_cpu(info
->EndOfFile
);
813 fattr
->cf_bytes
= le64_to_cpu(info
->AllocationSize
);
814 fattr
->cf_createtime
= le64_to_cpu(info
->CreationTime
);
815 fattr
->cf_nlink
= le32_to_cpu(info
->NumberOfLinks
);
816 fattr
->cf_uid
= cifs_sb
->ctx
->linux_uid
;
817 fattr
->cf_gid
= cifs_sb
->ctx
->linux_gid
;
819 fattr
->cf_mode
= cifs_sb
->ctx
->file_mode
;
820 if (cifs_open_data_reparse(data
) &&
821 cifs_reparse_point_to_fattr(cifs_sb
, fattr
, data
))
824 if (fattr
->cf_cifsattrs
& ATTR_DIRECTORY
) {
825 fattr
->cf_mode
= S_IFDIR
| cifs_sb
->ctx
->dir_mode
;
826 fattr
->cf_dtype
= DT_DIR
;
828 * Server can return wrong NumberOfLinks value for directories
829 * when Unix extensions are disabled - fake it.
832 fattr
->cf_flags
|= CIFS_FATTR_UNKNOWN_NLINK
;
834 fattr
->cf_mode
= S_IFREG
| cifs_sb
->ctx
->file_mode
;
835 fattr
->cf_dtype
= DT_REG
;
838 * Don't accept zero nlink from non-unix servers unless
839 * delete is pending. Instead mark it as unknown.
841 if ((fattr
->cf_nlink
< 1) && !tcon
->unix_ext
&&
842 !info
->DeletePending
) {
843 cifs_dbg(VFS
, "bogus file nlink value %u\n",
845 fattr
->cf_flags
|= CIFS_FATTR_UNKNOWN_NLINK
;
849 /* clear write bits if ATTR_READONLY is set */
850 if (fattr
->cf_cifsattrs
& ATTR_READONLY
)
851 fattr
->cf_mode
&= ~(S_IWUGO
);
854 if (S_ISLNK(fattr
->cf_mode
)) {
855 if (likely(data
->symlink_target
))
856 fattr
->cf_eof
= strnlen(data
->symlink_target
, PATH_MAX
);
857 fattr
->cf_symlink_target
= data
->symlink_target
;
858 data
->symlink_target
= NULL
;
863 cifs_get_file_info(struct file
*filp
)
867 struct cifs_open_info_data data
= {};
868 struct cifs_fattr fattr
;
869 struct inode
*inode
= file_inode(filp
);
870 struct cifsFileInfo
*cfile
= filp
->private_data
;
871 struct cifs_tcon
*tcon
= tlink_tcon(cfile
->tlink
);
872 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
873 struct dentry
*dentry
= filp
->f_path
.dentry
;
874 void *page
= alloc_dentry_path();
875 const unsigned char *path
;
877 if (!server
->ops
->query_file_info
) {
878 free_dentry_path(page
);
883 rc
= server
->ops
->query_file_info(xid
, tcon
, cfile
, &data
);
886 /* TODO: add support to query reparse tag */
887 data
.adjust_tz
= false;
888 if (data
.symlink_target
) {
890 data
.reparse
.tag
= IO_REPARSE_TAG_SYMLINK
;
892 path
= build_path_from_dentry(dentry
, page
);
897 cifs_open_info_to_fattr(&fattr
, &data
, inode
->i_sb
);
898 if (fattr
.cf_flags
& CIFS_FATTR_DELETE_PENDING
)
899 cifs_mark_open_handles_for_deleted_file(inode
, path
);
902 cifs_create_junction_fattr(&fattr
, inode
->i_sb
);
907 * FIXME: legacy server -- fall back to path-based call?
908 * for now, just skip revalidating and mark inode for
912 CIFS_I(inode
)->time
= 0;
919 * don't bother with SFU junk here -- just mark inode as needing
922 fattr
.cf_uniqueid
= CIFS_I(inode
)->uniqueid
;
923 fattr
.cf_flags
|= CIFS_FATTR_NEED_REVAL
;
924 /* if filetype is different, return error */
925 rc
= cifs_fattr_to_inode(inode
, &fattr
, false);
927 cifs_free_open_info(&data
);
928 free_dentry_path(page
);
933 /* Simple function to return a 64 bit hash of string. Rarely called */
934 static __u64
simple_hashstr(const char *str
)
936 const __u64 hash_mult
= 1125899906842597ULL; /* a big enough prime */
940 hash
= (hash
+ (__u64
) *str
++) * hash_mult
;
945 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
947 * cifs_backup_query_path_info - SMB1 fallback code to get ino
949 * Fallback code to get file metadata when we don't have access to
950 * full_path (EACCES) and have backup creds.
952 * @xid: transaction id used to identify original request in logs
953 * @tcon: information about the server share we have mounted
954 * @sb: the superblock stores info such as disk space available
955 * @full_path: name of the file we are getting the metadata for
956 * @resp_buf: will be set to cifs resp buf and needs to be freed with
957 * cifs_buf_release() when done with @data
958 * @data: will be set to search info result buffer
961 cifs_backup_query_path_info(int xid
,
962 struct cifs_tcon
*tcon
,
963 struct super_block
*sb
,
964 const char *full_path
,
966 FILE_ALL_INFO
**data
)
968 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
969 struct cifs_search_info info
= {0};
974 info
.endOfSearch
= false;
976 info
.info_level
= SMB_FIND_FILE_UNIX
;
977 else if ((tcon
->ses
->capabilities
&
978 tcon
->ses
->server
->vals
->cap_nt_find
) == 0)
979 info
.info_level
= SMB_FIND_FILE_INFO_STANDARD
;
980 else if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
)
981 info
.info_level
= SMB_FIND_FILE_ID_FULL_DIR_INFO
;
982 else /* no srvino useful for fallback to some netapp */
983 info
.info_level
= SMB_FIND_FILE_DIRECTORY_INFO
;
985 flags
= CIFS_SEARCH_CLOSE_ALWAYS
|
986 CIFS_SEARCH_CLOSE_AT_END
|
987 CIFS_SEARCH_BACKUP_SEARCH
;
989 rc
= CIFSFindFirst(xid
, tcon
, full_path
,
990 cifs_sb
, NULL
, flags
, &info
, false);
994 *resp_buf
= (void *)info
.ntwrk_buf_start
;
995 *data
= (FILE_ALL_INFO
*)info
.srch_entries_start
;
998 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1000 static void cifs_set_fattr_ino(int xid
, struct cifs_tcon
*tcon
, struct super_block
*sb
,
1001 struct inode
**inode
, const char *full_path
,
1002 struct cifs_open_info_data
*data
, struct cifs_fattr
*fattr
)
1004 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
1005 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
1008 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
)) {
1010 fattr
->cf_uniqueid
= CIFS_I(*inode
)->uniqueid
;
1012 fattr
->cf_uniqueid
= iunique(sb
, ROOT_I
);
1017 * If we have an inode pass a NULL tcon to ensure we don't
1018 * make a round trip to the server. This only works for SMB2+.
1020 rc
= server
->ops
->get_srv_inum(xid
, *inode
? NULL
: tcon
, cifs_sb
, full_path
,
1021 &fattr
->cf_uniqueid
, data
);
1024 * If that fails reuse existing ino or generate one
1025 * and disable server ones
1028 fattr
->cf_uniqueid
= CIFS_I(*inode
)->uniqueid
;
1030 fattr
->cf_uniqueid
= iunique(sb
, ROOT_I
);
1031 cifs_autodisable_serverino(cifs_sb
);
1036 /* If no errors, check for zero root inode (invalid) */
1037 if (fattr
->cf_uniqueid
== 0 && strlen(full_path
) == 0) {
1038 cifs_dbg(FYI
, "Invalid (0) inodenum\n");
1041 fattr
->cf_uniqueid
= CIFS_I(*inode
)->uniqueid
;
1043 /* make an ino by hashing the UNC */
1044 fattr
->cf_flags
|= CIFS_FATTR_FAKE_ROOT_INO
;
1045 fattr
->cf_uniqueid
= simple_hashstr(tcon
->tree_name
);
1050 static inline bool is_inode_cache_good(struct inode
*ino
)
1052 return ino
&& CIFS_CACHE_READ(CIFS_I(ino
)) && CIFS_I(ino
)->time
!= 0;
1055 static int reparse_info_to_fattr(struct cifs_open_info_data
*data
,
1056 struct super_block
*sb
,
1057 const unsigned int xid
,
1058 struct cifs_tcon
*tcon
,
1059 const char *full_path
,
1060 struct cifs_fattr
*fattr
)
1062 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
1063 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
1064 struct kvec rsp_iov
, *iov
= NULL
;
1065 int rsp_buftype
= CIFS_NO_BUFFER
;
1066 u32 tag
= data
->reparse
.tag
;
1069 if (!tag
&& server
->ops
->query_reparse_point
) {
1070 rc
= server
->ops
->query_reparse_point(xid
, tcon
, cifs_sb
,
1072 &rsp_iov
, &rsp_buftype
);
1075 } else if (data
->reparse
.io
.buftype
!= CIFS_NO_BUFFER
&&
1076 data
->reparse
.io
.iov
.iov_base
) {
1077 iov
= &data
->reparse
.io
.iov
;
1081 data
->reparse
.tag
= tag
;
1082 if (!data
->reparse
.tag
) {
1083 if (server
->ops
->query_symlink
) {
1084 rc
= server
->ops
->query_symlink(xid
, tcon
,
1086 &data
->symlink_target
);
1088 if (rc
== -EOPNOTSUPP
)
1089 data
->reparse
.tag
= IO_REPARSE_TAG_INTERNAL
;
1092 switch (data
->reparse
.tag
) {
1093 case 0: /* SMB1 symlink */
1095 case IO_REPARSE_TAG_INTERNAL
:
1097 if (le32_to_cpu(data
->fi
.Attributes
) & ATTR_DIRECTORY
) {
1098 cifs_create_junction_fattr(fattr
, sb
);
1102 case IO_REPARSE_TAG_MOUNT_POINT
:
1103 cifs_create_junction_fattr(fattr
, sb
);
1107 /* Check for cached reparse point data */
1108 if (data
->symlink_target
|| data
->reparse
.buf
) {
1110 } else if (iov
&& server
->ops
->parse_reparse_point
) {
1111 rc
= server
->ops
->parse_reparse_point(cifs_sb
,
1117 if (tcon
->posix_extensions
)
1118 smb311_posix_info_to_fattr(fattr
, data
, sb
);
1120 cifs_open_info_to_fattr(fattr
, data
, sb
);
1122 fattr
->cf_cifstag
= data
->reparse
.tag
;
1123 free_rsp_buf(rsp_buftype
, rsp_iov
.iov_base
);
1127 static int cifs_get_fattr(struct cifs_open_info_data
*data
,
1128 struct super_block
*sb
, int xid
,
1129 const struct cifs_fid
*fid
,
1130 struct cifs_fattr
*fattr
,
1131 struct inode
**inode
,
1132 const char *full_path
)
1134 struct cifs_open_info_data tmp_data
= {};
1135 struct cifs_tcon
*tcon
;
1136 struct TCP_Server_Info
*server
;
1137 struct tcon_link
*tlink
;
1138 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
1139 void *smb1_backup_rsp_buf
= NULL
;
1143 tlink
= cifs_sb_tlink(cifs_sb
);
1145 return PTR_ERR(tlink
);
1146 tcon
= tlink_tcon(tlink
);
1147 server
= tcon
->ses
->server
;
1150 * 1. Fetch file metadata if not provided (data)
1154 rc
= server
->ops
->query_path_info(xid
, tcon
, cifs_sb
,
1155 full_path
, &tmp_data
);
1160 * 2. Convert it to internal cifs metadata (fattr)
1166 * If the file is a reparse point, it is more complicated
1167 * since we have to check if its reparse tag matches a known
1168 * special file type e.g. symlink or fifo or char etc.
1170 if (cifs_open_data_reparse(data
)) {
1171 rc
= reparse_info_to_fattr(data
, sb
, xid
, tcon
,
1174 cifs_open_info_to_fattr(fattr
, data
, sb
);
1176 if (!rc
&& *inode
&&
1177 (fattr
->cf_flags
& CIFS_FATTR_DELETE_PENDING
))
1178 cifs_mark_open_handles_for_deleted_file(*inode
, full_path
);
1181 /* DFS link, no metadata available on this server */
1182 cifs_create_junction_fattr(fattr
, sb
);
1186 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1188 * perm errors, try again with backup flags if possible
1190 * For SMB2 and later the backup intent flag
1191 * is already sent if needed on open and there
1192 * is no path based FindFirst operation to use
1195 if (backup_cred(cifs_sb
) && is_smb1_server(server
)) {
1196 /* for easier reading */
1198 FILE_DIRECTORY_INFO
*fdi
;
1199 SEARCH_ID_FULL_DIR_INFO
*si
;
1201 rc
= cifs_backup_query_path_info(xid
, tcon
, sb
,
1203 &smb1_backup_rsp_buf
,
1208 move_cifs_info_to_smb2(&data
->fi
, fi
);
1209 fdi
= (FILE_DIRECTORY_INFO
*)fi
;
1210 si
= (SEARCH_ID_FULL_DIR_INFO
*)fi
;
1212 cifs_dir_info_to_fattr(fattr
, fdi
, cifs_sb
);
1213 fattr
->cf_uniqueid
= le64_to_cpu(si
->UniqueId
);
1214 /* uniqueid set, skip get inum step */
1215 goto handle_mnt_opt
;
1217 /* nothing we can do, bail out */
1222 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1225 cifs_dbg(FYI
, "%s: unhandled err rc %d\n", __func__
, rc
);
1230 * 3. Get or update inode number (fattr->cf_uniqueid)
1233 cifs_set_fattr_ino(xid
, tcon
, sb
, inode
, full_path
, data
, fattr
);
1236 * 4. Tweak fattr based on mount options
1238 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1240 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1241 /* query for SFU type info if supported and needed */
1242 if ((fattr
->cf_cifsattrs
& ATTR_SYSTEM
) &&
1243 (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_UNX_EMUL
)) {
1244 tmprc
= cifs_sfu_type(fattr
, full_path
, cifs_sb
, xid
);
1246 cifs_dbg(FYI
, "cifs_sfu_type failed: %d\n", tmprc
);
1249 /* fill in 0777 bits from ACL */
1250 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MODE_FROM_SID
) {
1251 rc
= cifs_acl_to_fattr(cifs_sb
, fattr
, *inode
,
1252 true, full_path
, fid
);
1256 cifs_dbg(FYI
, "%s: Get mode from SID failed. rc=%d\n",
1260 } else if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_CIFS_ACL
) {
1261 rc
= cifs_acl_to_fattr(cifs_sb
, fattr
, *inode
,
1262 false, full_path
, fid
);
1266 cifs_dbg(FYI
, "%s: Getting ACL failed with error: %d\n",
1270 } else if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_UNX_EMUL
)
1271 /* fill in remaining high mode bits e.g. SUID, VTX */
1272 cifs_sfu_mode(fattr
, full_path
, cifs_sb
, xid
);
1273 else if (!(tcon
->posix_extensions
))
1274 /* clear write bits if ATTR_READONLY is set */
1275 if (fattr
->cf_cifsattrs
& ATTR_READONLY
)
1276 fattr
->cf_mode
&= ~(S_IWUGO
);
1279 /* check for Minshall+French symlinks */
1280 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MF_SYMLINKS
) {
1281 tmprc
= check_mf_symlink(xid
, tcon
, cifs_sb
, fattr
, full_path
);
1282 cifs_dbg(FYI
, "check_mf_symlink: %d\n", tmprc
);
1286 cifs_buf_release(smb1_backup_rsp_buf
);
1287 cifs_put_tlink(tlink
);
1288 cifs_free_open_info(&tmp_data
);
1292 int cifs_get_inode_info(struct inode
**inode
,
1293 const char *full_path
,
1294 struct cifs_open_info_data
*data
,
1295 struct super_block
*sb
, int xid
,
1296 const struct cifs_fid
*fid
)
1298 struct cifs_fattr fattr
= {};
1301 if (is_inode_cache_good(*inode
)) {
1302 cifs_dbg(FYI
, "No need to revalidate cached inode sizes\n");
1306 rc
= cifs_get_fattr(data
, sb
, xid
, fid
, &fattr
, inode
, full_path
);
1310 rc
= update_inode_info(sb
, &fattr
, inode
);
1312 kfree(fattr
.cf_symlink_target
);
1316 static int smb311_posix_get_fattr(struct cifs_open_info_data
*data
,
1317 struct cifs_fattr
*fattr
,
1318 const char *full_path
,
1319 struct super_block
*sb
,
1320 const unsigned int xid
)
1322 struct cifs_open_info_data tmp_data
= {};
1323 struct TCP_Server_Info
*server
;
1324 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
1325 struct cifs_tcon
*tcon
;
1326 struct tcon_link
*tlink
;
1330 tlink
= cifs_sb_tlink(cifs_sb
);
1332 return PTR_ERR(tlink
);
1333 tcon
= tlink_tcon(tlink
);
1334 server
= tcon
->ses
->server
;
1337 * 1. Fetch file metadata if not provided (data)
1340 rc
= server
->ops
->query_path_info(xid
, tcon
, cifs_sb
,
1341 full_path
, &tmp_data
);
1346 * 2. Convert it to internal cifs metadata (fattr)
1351 if (cifs_open_data_reparse(data
)) {
1352 rc
= reparse_info_to_fattr(data
, sb
, xid
, tcon
,
1355 smb311_posix_info_to_fattr(fattr
, data
, sb
);
1359 /* DFS link, no metadata available on this server */
1360 cifs_create_junction_fattr(fattr
, sb
);
1365 * For SMB2 and later the backup intent flag
1366 * is already sent if needed on open and there
1367 * is no path based FindFirst operation to use
1368 * to retry with so nothing we can do, bail out
1372 cifs_dbg(FYI
, "%s: unhandled err rc %d\n", __func__
, rc
);
1377 * 3. Tweak fattr based on mount options
1379 /* check for Minshall+French symlinks */
1380 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MF_SYMLINKS
) {
1381 tmprc
= check_mf_symlink(xid
, tcon
, cifs_sb
, fattr
, full_path
);
1382 cifs_dbg(FYI
, "check_mf_symlink: %d\n", tmprc
);
1386 cifs_put_tlink(tlink
);
1387 cifs_free_open_info(data
);
1391 int smb311_posix_get_inode_info(struct inode
**inode
,
1392 const char *full_path
,
1393 struct cifs_open_info_data
*data
,
1394 struct super_block
*sb
,
1395 const unsigned int xid
)
1397 struct cifs_fattr fattr
= {};
1400 if (is_inode_cache_good(*inode
)) {
1401 cifs_dbg(FYI
, "No need to revalidate cached inode sizes\n");
1405 rc
= smb311_posix_get_fattr(data
, &fattr
, full_path
, sb
, xid
);
1409 rc
= update_inode_info(sb
, &fattr
, inode
);
1410 if (!rc
&& fattr
.cf_flags
& CIFS_FATTR_DELETE_PENDING
)
1411 cifs_mark_open_handles_for_deleted_file(*inode
, full_path
);
1413 kfree(fattr
.cf_symlink_target
);
1417 static const struct inode_operations cifs_ipc_inode_ops
= {
1418 .lookup
= cifs_lookup
,
1422 cifs_find_inode(struct inode
*inode
, void *opaque
)
1424 struct cifs_fattr
*fattr
= opaque
;
1426 /* [!] The compared values must be the same in struct cifs_fscache_inode_key. */
1428 /* don't match inode with different uniqueid */
1429 if (CIFS_I(inode
)->uniqueid
!= fattr
->cf_uniqueid
)
1432 /* use createtime like an i_generation field */
1433 if (CIFS_I(inode
)->createtime
!= fattr
->cf_createtime
)
1436 /* don't match inode of different type */
1437 if (inode_wrong_type(inode
, fattr
->cf_mode
))
1440 /* if it's not a directory or has no dentries, then flag it */
1441 if (S_ISDIR(inode
->i_mode
) && !hlist_empty(&inode
->i_dentry
))
1442 fattr
->cf_flags
|= CIFS_FATTR_INO_COLLISION
;
1448 cifs_init_inode(struct inode
*inode
, void *opaque
)
1450 struct cifs_fattr
*fattr
= opaque
;
1452 CIFS_I(inode
)->uniqueid
= fattr
->cf_uniqueid
;
1453 CIFS_I(inode
)->createtime
= fattr
->cf_createtime
;
1458 * walk dentry list for an inode and report whether it has aliases that
1459 * are hashed. We use this to determine if a directory inode can actually
1463 inode_has_hashed_dentries(struct inode
*inode
)
1465 struct dentry
*dentry
;
1467 spin_lock(&inode
->i_lock
);
1468 hlist_for_each_entry(dentry
, &inode
->i_dentry
, d_u
.d_alias
) {
1469 if (!d_unhashed(dentry
) || IS_ROOT(dentry
)) {
1470 spin_unlock(&inode
->i_lock
);
1474 spin_unlock(&inode
->i_lock
);
1478 /* Given fattrs, get a corresponding inode */
1480 cifs_iget(struct super_block
*sb
, struct cifs_fattr
*fattr
)
1483 struct inode
*inode
;
1486 cifs_dbg(FYI
, "looking for uniqueid=%llu\n", fattr
->cf_uniqueid
);
1488 /* hash down to 32-bits on 32-bit arch */
1489 hash
= cifs_uniqueid_to_ino_t(fattr
->cf_uniqueid
);
1491 inode
= iget5_locked(sb
, hash
, cifs_find_inode
, cifs_init_inode
, fattr
);
1493 /* was there a potentially problematic inode collision? */
1494 if (fattr
->cf_flags
& CIFS_FATTR_INO_COLLISION
) {
1495 fattr
->cf_flags
&= ~CIFS_FATTR_INO_COLLISION
;
1497 if (inode_has_hashed_dentries(inode
)) {
1498 cifs_autodisable_serverino(CIFS_SB(sb
));
1500 fattr
->cf_uniqueid
= iunique(sb
, ROOT_I
);
1501 goto retry_iget5_locked
;
1505 /* can't fail - see cifs_find_inode() */
1506 cifs_fattr_to_inode(inode
, fattr
, false);
1507 if (sb
->s_flags
& SB_NOATIME
)
1508 inode
->i_flags
|= S_NOATIME
| S_NOCMTIME
;
1509 if (inode
->i_state
& I_NEW
) {
1510 inode
->i_ino
= hash
;
1511 cifs_fscache_get_inode_cookie(inode
);
1512 unlock_new_inode(inode
);
1519 /* gets root inode */
1520 struct inode
*cifs_root_iget(struct super_block
*sb
)
1522 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
1523 struct cifs_fattr fattr
= {};
1524 struct cifs_tcon
*tcon
= cifs_sb_master_tcon(cifs_sb
);
1525 struct inode
*inode
= NULL
;
1531 if ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_USE_PREFIX_PATH
)
1532 && cifs_sb
->prepath
) {
1533 len
= strlen(cifs_sb
->prepath
);
1534 path
= kzalloc(len
+ 2 /* leading sep + null */, GFP_KERNEL
);
1536 return ERR_PTR(-ENOMEM
);
1538 memcpy(path
+1, cifs_sb
->prepath
, len
);
1540 path
= kstrdup("", GFP_KERNEL
);
1542 return ERR_PTR(-ENOMEM
);
1546 if (tcon
->unix_ext
) {
1547 rc
= cifs_get_unix_fattr(path
, sb
, &fattr
, &inode
, xid
);
1548 /* some servers mistakenly claim POSIX support */
1549 if (rc
!= -EOPNOTSUPP
)
1551 cifs_dbg(VFS
, "server does not support POSIX extensions\n");
1552 tcon
->unix_ext
= false;
1555 convert_delimiter(path
, CIFS_DIR_SEP(cifs_sb
));
1556 if (tcon
->posix_extensions
)
1557 rc
= smb311_posix_get_fattr(NULL
, &fattr
, path
, sb
, xid
);
1559 rc
= cifs_get_fattr(NULL
, sb
, xid
, NULL
, &fattr
, &inode
, path
);
1563 if (fattr
.cf_flags
& CIFS_FATTR_JUNCTION
) {
1564 fattr
.cf_flags
&= ~CIFS_FATTR_JUNCTION
;
1565 cifs_autodisable_serverino(cifs_sb
);
1567 inode
= cifs_iget(sb
, &fattr
);
1571 inode
= ERR_PTR(rc
);
1575 if (!rc
&& fattr
.cf_flags
& CIFS_FATTR_DELETE_PENDING
)
1576 cifs_mark_open_handles_for_deleted_file(inode
, path
);
1578 if (rc
&& tcon
->pipe
) {
1579 cifs_dbg(FYI
, "ipc connection - fake read inode\n");
1580 spin_lock(&inode
->i_lock
);
1581 inode
->i_mode
|= S_IFDIR
;
1582 set_nlink(inode
, 2);
1583 inode
->i_op
= &cifs_ipc_inode_ops
;
1584 inode
->i_fop
= &simple_dir_operations
;
1585 inode
->i_uid
= cifs_sb
->ctx
->linux_uid
;
1586 inode
->i_gid
= cifs_sb
->ctx
->linux_gid
;
1587 spin_unlock(&inode
->i_lock
);
1590 inode
= ERR_PTR(rc
);
1596 kfree(fattr
.cf_symlink_target
);
1601 cifs_set_file_info(struct inode
*inode
, struct iattr
*attrs
, unsigned int xid
,
1602 const char *full_path
, __u32 dosattr
)
1604 bool set_time
= false;
1605 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
1606 struct TCP_Server_Info
*server
;
1607 FILE_BASIC_INFO info_buf
;
1612 server
= cifs_sb_master_tcon(cifs_sb
)->ses
->server
;
1613 if (!server
->ops
->set_file_info
)
1618 if (attrs
->ia_valid
& ATTR_ATIME
) {
1620 info_buf
.LastAccessTime
=
1621 cpu_to_le64(cifs_UnixTimeToNT(attrs
->ia_atime
));
1623 info_buf
.LastAccessTime
= 0;
1625 if (attrs
->ia_valid
& ATTR_MTIME
) {
1627 info_buf
.LastWriteTime
=
1628 cpu_to_le64(cifs_UnixTimeToNT(attrs
->ia_mtime
));
1630 info_buf
.LastWriteTime
= 0;
1633 * Samba throws this field away, but windows may actually use it.
1634 * Do not set ctime unless other time stamps are changed explicitly
1635 * (i.e. by utimes()) since we would then have a mix of client and
1638 if (set_time
&& (attrs
->ia_valid
& ATTR_CTIME
)) {
1639 cifs_dbg(FYI
, "CIFS - CTIME changed\n");
1640 info_buf
.ChangeTime
=
1641 cpu_to_le64(cifs_UnixTimeToNT(attrs
->ia_ctime
));
1643 info_buf
.ChangeTime
= 0;
1645 info_buf
.CreationTime
= 0; /* don't change */
1646 info_buf
.Attributes
= cpu_to_le32(dosattr
);
1648 return server
->ops
->set_file_info(inode
, full_path
, &info_buf
, xid
);
1651 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1653 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1654 * and rename it to a random name that hopefully won't conflict with
1658 cifs_rename_pending_delete(const char *full_path
, struct dentry
*dentry
,
1659 const unsigned int xid
)
1663 struct cifs_fid fid
;
1664 struct cifs_open_parms oparms
;
1665 struct inode
*inode
= d_inode(dentry
);
1666 struct cifsInodeInfo
*cifsInode
= CIFS_I(inode
);
1667 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
1668 struct tcon_link
*tlink
;
1669 struct cifs_tcon
*tcon
;
1670 __u32 dosattr
, origattr
;
1671 FILE_BASIC_INFO
*info_buf
= NULL
;
1673 tlink
= cifs_sb_tlink(cifs_sb
);
1675 return PTR_ERR(tlink
);
1676 tcon
= tlink_tcon(tlink
);
1679 * We cannot rename the file if the server doesn't support
1680 * CAP_INFOLEVEL_PASSTHRU
1682 if (!(tcon
->ses
->capabilities
& CAP_INFOLEVEL_PASSTHRU
)) {
1687 oparms
= (struct cifs_open_parms
) {
1690 .desired_access
= DELETE
| FILE_WRITE_ATTRIBUTES
,
1691 .create_options
= cifs_create_options(cifs_sb
, CREATE_NOT_DIR
),
1692 .disposition
= FILE_OPEN
,
1697 rc
= CIFS_open(xid
, &oparms
, &oplock
, NULL
);
1701 origattr
= cifsInode
->cifsAttrs
;
1703 origattr
|= ATTR_NORMAL
;
1705 dosattr
= origattr
& ~ATTR_READONLY
;
1707 dosattr
|= ATTR_NORMAL
;
1708 dosattr
|= ATTR_HIDDEN
;
1710 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1711 if (dosattr
!= origattr
) {
1712 info_buf
= kzalloc(sizeof(*info_buf
), GFP_KERNEL
);
1713 if (info_buf
== NULL
) {
1717 info_buf
->Attributes
= cpu_to_le32(dosattr
);
1718 rc
= CIFSSMBSetFileInfo(xid
, tcon
, info_buf
, fid
.netfid
,
1720 /* although we would like to mark the file hidden
1721 if that fails we will still try to rename it */
1723 cifsInode
->cifsAttrs
= dosattr
;
1725 dosattr
= origattr
; /* since not able to change them */
1728 /* rename the file */
1729 rc
= CIFSSMBRenameOpenFile(xid
, tcon
, fid
.netfid
, NULL
,
1731 cifs_remap(cifs_sb
));
1737 /* try to set DELETE_ON_CLOSE */
1738 if (!test_bit(CIFS_INO_DELETE_PENDING
, &cifsInode
->flags
)) {
1739 rc
= CIFSSMBSetFileDisposition(xid
, tcon
, true, fid
.netfid
,
1742 * some samba versions return -ENOENT when we try to set the
1743 * file disposition here. Likely a samba bug, but work around
1744 * it for now. This means that some cifsXXX files may hang
1745 * around after they shouldn't.
1747 * BB: remove this hack after more servers have the fix
1755 set_bit(CIFS_INO_DELETE_PENDING
, &cifsInode
->flags
);
1759 CIFSSMBClose(xid
, tcon
, fid
.netfid
);
1762 cifs_put_tlink(tlink
);
1766 * reset everything back to the original state. Don't bother
1767 * dealing with errors here since we can't do anything about
1771 CIFSSMBRenameOpenFile(xid
, tcon
, fid
.netfid
, dentry
->d_name
.name
,
1772 cifs_sb
->local_nls
, cifs_remap(cifs_sb
));
1774 if (dosattr
!= origattr
) {
1775 info_buf
->Attributes
= cpu_to_le32(origattr
);
1776 if (!CIFSSMBSetFileInfo(xid
, tcon
, info_buf
, fid
.netfid
,
1778 cifsInode
->cifsAttrs
= origattr
;
1783 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1785 /* copied from fs/nfs/dir.c with small changes */
1787 cifs_drop_nlink(struct inode
*inode
)
1789 spin_lock(&inode
->i_lock
);
1790 if (inode
->i_nlink
> 0)
1792 spin_unlock(&inode
->i_lock
);
1796 * If d_inode(dentry) is null (usually meaning the cached dentry
1797 * is a negative dentry) then we would attempt a standard SMB delete, but
1798 * if that fails we can not attempt the fall back mechanisms on EACCES
1799 * but will return the EACCES to the caller. Note that the VFS does not call
1800 * unlink on negative dentries currently.
1802 int cifs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1806 const char *full_path
;
1808 struct inode
*inode
= d_inode(dentry
);
1809 struct cifsInodeInfo
*cifs_inode
;
1810 struct super_block
*sb
= dir
->i_sb
;
1811 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
1812 struct tcon_link
*tlink
;
1813 struct cifs_tcon
*tcon
;
1814 struct TCP_Server_Info
*server
;
1815 struct iattr
*attrs
= NULL
;
1816 __u32 dosattr
= 0, origattr
= 0;
1818 cifs_dbg(FYI
, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir
, dentry
);
1820 if (unlikely(cifs_forced_shutdown(cifs_sb
)))
1823 tlink
= cifs_sb_tlink(cifs_sb
);
1825 return PTR_ERR(tlink
);
1826 tcon
= tlink_tcon(tlink
);
1827 server
= tcon
->ses
->server
;
1830 page
= alloc_dentry_path();
1832 if (tcon
->nodelete
) {
1837 /* Unlink can be called from rename so we can not take the
1838 * sb->s_vfs_rename_mutex here */
1839 full_path
= build_path_from_dentry(dentry
, page
);
1840 if (IS_ERR(full_path
)) {
1841 rc
= PTR_ERR(full_path
);
1845 cifs_close_deferred_file_under_dentry(tcon
, full_path
);
1846 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1847 if (cap_unix(tcon
->ses
) && (CIFS_UNIX_POSIX_PATH_OPS_CAP
&
1848 le64_to_cpu(tcon
->fsUnixInfo
.Capability
))) {
1849 rc
= CIFSPOSIXDelFile(xid
, tcon
, full_path
,
1850 SMB_POSIX_UNLINK_FILE_TARGET
, cifs_sb
->local_nls
,
1851 cifs_remap(cifs_sb
));
1852 cifs_dbg(FYI
, "posix del rc %d\n", rc
);
1853 if ((rc
== 0) || (rc
== -ENOENT
))
1854 goto psx_del_no_retry
;
1856 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1859 if (!server
->ops
->unlink
) {
1861 goto psx_del_no_retry
;
1864 rc
= server
->ops
->unlink(xid
, tcon
, full_path
, cifs_sb
, dentry
);
1869 cifs_mark_open_handles_for_deleted_file(inode
, full_path
);
1870 cifs_drop_nlink(inode
);
1872 } else if (rc
== -ENOENT
) {
1874 } else if (rc
== -EBUSY
) {
1875 if (server
->ops
->rename_pending_delete
) {
1876 rc
= server
->ops
->rename_pending_delete(full_path
,
1879 cifs_mark_open_handles_for_deleted_file(inode
, full_path
);
1880 cifs_drop_nlink(inode
);
1883 } else if ((rc
== -EACCES
) && (dosattr
== 0) && inode
) {
1884 attrs
= kzalloc(sizeof(*attrs
), GFP_KERNEL
);
1885 if (attrs
== NULL
) {
1890 /* try to reset dos attributes */
1891 cifs_inode
= CIFS_I(inode
);
1892 origattr
= cifs_inode
->cifsAttrs
;
1894 origattr
|= ATTR_NORMAL
;
1895 dosattr
= origattr
& ~ATTR_READONLY
;
1897 dosattr
|= ATTR_NORMAL
;
1898 dosattr
|= ATTR_HIDDEN
;
1900 rc
= cifs_set_file_info(inode
, attrs
, xid
, full_path
, dosattr
);
1904 goto retry_std_delete
;
1907 /* undo the setattr if we errored out and it's needed */
1908 if (rc
!= 0 && dosattr
!= 0)
1909 cifs_set_file_info(inode
, attrs
, xid
, full_path
, origattr
);
1913 cifs_inode
= CIFS_I(inode
);
1914 cifs_inode
->time
= 0; /* will force revalidate to get info
1916 inode_set_ctime_current(inode
);
1918 inode_set_mtime_to_ts(dir
, inode_set_ctime_current(dir
));
1919 cifs_inode
= CIFS_I(dir
);
1920 CIFS_I(dir
)->time
= 0; /* force revalidate of dir as well */
1922 free_dentry_path(page
);
1925 cifs_put_tlink(tlink
);
1930 cifs_mkdir_qinfo(struct inode
*parent
, struct dentry
*dentry
, umode_t mode
,
1931 const char *full_path
, struct cifs_sb_info
*cifs_sb
,
1932 struct cifs_tcon
*tcon
, const unsigned int xid
)
1935 struct inode
*inode
= NULL
;
1937 if (tcon
->posix_extensions
) {
1938 rc
= smb311_posix_get_inode_info(&inode
, full_path
,
1939 NULL
, parent
->i_sb
, xid
);
1940 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1941 } else if (tcon
->unix_ext
) {
1942 rc
= cifs_get_inode_info_unix(&inode
, full_path
, parent
->i_sb
,
1944 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1946 rc
= cifs_get_inode_info(&inode
, full_path
, NULL
, parent
->i_sb
,
1953 if (!S_ISDIR(inode
->i_mode
)) {
1955 * mkdir succeeded, but another client has managed to remove the
1956 * sucker and replace it with non-directory. Return success,
1957 * but don't leave the child in dcache.
1964 * setting nlink not necessary except in cases where we failed to get it
1965 * from the server or was set bogus. Also, since this is a brand new
1966 * inode, no need to grab the i_lock before setting the i_nlink.
1968 if (inode
->i_nlink
< 2)
1969 set_nlink(inode
, 2);
1970 mode
&= ~current_umask();
1971 /* must turn on setgid bit if parent dir has it */
1972 if (parent
->i_mode
& S_ISGID
)
1975 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1976 if (tcon
->unix_ext
) {
1977 struct cifs_unix_set_info_args args
= {
1979 .ctime
= NO_CHANGE_64
,
1980 .atime
= NO_CHANGE_64
,
1981 .mtime
= NO_CHANGE_64
,
1984 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SET_UID
) {
1985 args
.uid
= current_fsuid();
1986 if (parent
->i_mode
& S_ISGID
)
1987 args
.gid
= parent
->i_gid
;
1989 args
.gid
= current_fsgid();
1991 args
.uid
= INVALID_UID
; /* no change */
1992 args
.gid
= INVALID_GID
; /* no change */
1994 CIFSSMBUnixSetPathInfo(xid
, tcon
, full_path
, &args
,
1996 cifs_remap(cifs_sb
));
2000 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2001 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2002 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_CIFS_ACL
) &&
2003 (mode
& S_IWUGO
) == 0 && server
->ops
->mkdir_setinfo
)
2004 server
->ops
->mkdir_setinfo(inode
, full_path
, cifs_sb
,
2006 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_DYNPERM
)
2007 inode
->i_mode
= (mode
| S_IFDIR
);
2009 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SET_UID
) {
2010 inode
->i_uid
= current_fsuid();
2011 if (inode
->i_mode
& S_ISGID
)
2012 inode
->i_gid
= parent
->i_gid
;
2014 inode
->i_gid
= current_fsgid();
2017 d_instantiate(dentry
, inode
);
2021 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2023 cifs_posix_mkdir(struct inode
*inode
, struct dentry
*dentry
, umode_t mode
,
2024 const char *full_path
, struct cifs_sb_info
*cifs_sb
,
2025 struct cifs_tcon
*tcon
, const unsigned int xid
)
2029 FILE_UNIX_BASIC_INFO
*info
= NULL
;
2030 struct inode
*newinode
= NULL
;
2031 struct cifs_fattr fattr
;
2033 info
= kzalloc(sizeof(FILE_UNIX_BASIC_INFO
), GFP_KERNEL
);
2036 goto posix_mkdir_out
;
2039 mode
&= ~current_umask();
2040 rc
= CIFSPOSIXCreate(xid
, tcon
, SMB_O_DIRECTORY
| SMB_O_CREAT
, mode
,
2041 NULL
/* netfid */, info
, &oplock
, full_path
,
2042 cifs_sb
->local_nls
, cifs_remap(cifs_sb
));
2043 if (rc
== -EOPNOTSUPP
)
2044 goto posix_mkdir_out
;
2046 cifs_dbg(FYI
, "posix mkdir returned 0x%x\n", rc
);
2048 goto posix_mkdir_out
;
2051 if (info
->Type
== cpu_to_le32(-1))
2052 /* no return info, go query for it */
2053 goto posix_mkdir_get_info
;
2055 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
2056 * need to set uid/gid.
2059 cifs_unix_basic_to_fattr(&fattr
, info
, cifs_sb
);
2060 cifs_fill_uniqueid(inode
->i_sb
, &fattr
);
2061 newinode
= cifs_iget(inode
->i_sb
, &fattr
);
2063 goto posix_mkdir_get_info
;
2065 d_instantiate(dentry
, newinode
);
2067 #ifdef CONFIG_CIFS_DEBUG2
2068 cifs_dbg(FYI
, "instantiated dentry %p %pd to inode %p\n",
2069 dentry
, dentry
, newinode
);
2071 if (newinode
->i_nlink
!= 2)
2072 cifs_dbg(FYI
, "unexpected number of links %d\n",
2079 posix_mkdir_get_info
:
2080 rc
= cifs_mkdir_qinfo(inode
, dentry
, mode
, full_path
, cifs_sb
, tcon
,
2082 goto posix_mkdir_out
;
2084 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2086 int cifs_mkdir(struct mnt_idmap
*idmap
, struct inode
*inode
,
2087 struct dentry
*direntry
, umode_t mode
)
2091 struct cifs_sb_info
*cifs_sb
;
2092 struct tcon_link
*tlink
;
2093 struct cifs_tcon
*tcon
;
2094 struct TCP_Server_Info
*server
;
2095 const char *full_path
;
2098 cifs_dbg(FYI
, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
2101 cifs_sb
= CIFS_SB(inode
->i_sb
);
2102 if (unlikely(cifs_forced_shutdown(cifs_sb
)))
2104 tlink
= cifs_sb_tlink(cifs_sb
);
2106 return PTR_ERR(tlink
);
2107 tcon
= tlink_tcon(tlink
);
2111 page
= alloc_dentry_path();
2112 full_path
= build_path_from_dentry(direntry
, page
);
2113 if (IS_ERR(full_path
)) {
2114 rc
= PTR_ERR(full_path
);
2118 server
= tcon
->ses
->server
;
2120 if ((server
->ops
->posix_mkdir
) && (tcon
->posix_extensions
)) {
2121 rc
= server
->ops
->posix_mkdir(xid
, inode
, mode
, tcon
, full_path
,
2123 d_drop(direntry
); /* for time being always refresh inode info */
2127 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2128 if (cap_unix(tcon
->ses
) && (CIFS_UNIX_POSIX_PATH_OPS_CAP
&
2129 le64_to_cpu(tcon
->fsUnixInfo
.Capability
))) {
2130 rc
= cifs_posix_mkdir(inode
, direntry
, mode
, full_path
, cifs_sb
,
2132 if (rc
!= -EOPNOTSUPP
)
2135 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2137 if (!server
->ops
->mkdir
) {
2142 /* BB add setting the equivalent of mode via CreateX w/ACLs */
2143 rc
= server
->ops
->mkdir(xid
, inode
, mode
, tcon
, full_path
, cifs_sb
);
2145 cifs_dbg(FYI
, "cifs_mkdir returned 0x%x\n", rc
);
2150 /* TODO: skip this for smb2/smb3 */
2151 rc
= cifs_mkdir_qinfo(inode
, direntry
, mode
, full_path
, cifs_sb
, tcon
,
2155 * Force revalidate to get parent dir info when needed since cached
2156 * attributes are invalid now.
2158 CIFS_I(inode
)->time
= 0;
2159 free_dentry_path(page
);
2161 cifs_put_tlink(tlink
);
2165 int cifs_rmdir(struct inode
*inode
, struct dentry
*direntry
)
2169 struct cifs_sb_info
*cifs_sb
;
2170 struct tcon_link
*tlink
;
2171 struct cifs_tcon
*tcon
;
2172 struct TCP_Server_Info
*server
;
2173 const char *full_path
;
2174 void *page
= alloc_dentry_path();
2175 struct cifsInodeInfo
*cifsInode
;
2177 cifs_dbg(FYI
, "cifs_rmdir, inode = 0x%p\n", inode
);
2181 full_path
= build_path_from_dentry(direntry
, page
);
2182 if (IS_ERR(full_path
)) {
2183 rc
= PTR_ERR(full_path
);
2187 cifs_sb
= CIFS_SB(inode
->i_sb
);
2188 if (unlikely(cifs_forced_shutdown(cifs_sb
))) {
2193 tlink
= cifs_sb_tlink(cifs_sb
);
2194 if (IS_ERR(tlink
)) {
2195 rc
= PTR_ERR(tlink
);
2198 tcon
= tlink_tcon(tlink
);
2199 server
= tcon
->ses
->server
;
2201 if (!server
->ops
->rmdir
) {
2203 cifs_put_tlink(tlink
);
2207 if (tcon
->nodelete
) {
2209 cifs_put_tlink(tlink
);
2213 rc
= server
->ops
->rmdir(xid
, tcon
, full_path
, cifs_sb
);
2214 cifs_put_tlink(tlink
);
2217 spin_lock(&d_inode(direntry
)->i_lock
);
2218 i_size_write(d_inode(direntry
), 0);
2219 clear_nlink(d_inode(direntry
));
2220 spin_unlock(&d_inode(direntry
)->i_lock
);
2223 cifsInode
= CIFS_I(d_inode(direntry
));
2224 /* force revalidate to go get info when needed */
2225 cifsInode
->time
= 0;
2227 cifsInode
= CIFS_I(inode
);
2229 * Force revalidate to get parent dir info when needed since cached
2230 * attributes are invalid now.
2232 cifsInode
->time
= 0;
2234 inode_set_ctime_current(d_inode(direntry
));
2235 inode_set_mtime_to_ts(inode
, inode_set_ctime_current(inode
));
2238 free_dentry_path(page
);
2244 cifs_do_rename(const unsigned int xid
, struct dentry
*from_dentry
,
2245 const char *from_path
, struct dentry
*to_dentry
,
2246 const char *to_path
)
2248 struct cifs_sb_info
*cifs_sb
= CIFS_SB(from_dentry
->d_sb
);
2249 struct tcon_link
*tlink
;
2250 struct cifs_tcon
*tcon
;
2251 struct TCP_Server_Info
*server
;
2252 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2253 struct cifs_fid fid
;
2254 struct cifs_open_parms oparms
;
2256 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2259 tlink
= cifs_sb_tlink(cifs_sb
);
2261 return PTR_ERR(tlink
);
2262 tcon
= tlink_tcon(tlink
);
2263 server
= tcon
->ses
->server
;
2265 if (!server
->ops
->rename
)
2268 /* try path-based rename first */
2269 rc
= server
->ops
->rename(xid
, tcon
, from_dentry
,
2270 from_path
, to_path
, cifs_sb
);
2273 * Don't bother with rename by filehandle unless file is busy and
2274 * source. Note that cross directory moves do not work with
2275 * rename by filehandle to various Windows servers.
2277 if (rc
== 0 || rc
!= -EBUSY
)
2278 goto do_rename_exit
;
2280 /* Don't fall back to using SMB on SMB 2+ mount */
2281 if (server
->vals
->protocol_id
!= 0)
2282 goto do_rename_exit
;
2284 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2285 /* open-file renames don't work across directories */
2286 if (to_dentry
->d_parent
!= from_dentry
->d_parent
)
2287 goto do_rename_exit
;
2289 oparms
= (struct cifs_open_parms
) {
2292 /* open the file to be renamed -- we need DELETE perms */
2293 .desired_access
= DELETE
,
2294 .create_options
= cifs_create_options(cifs_sb
, CREATE_NOT_DIR
),
2295 .disposition
= FILE_OPEN
,
2300 rc
= CIFS_open(xid
, &oparms
, &oplock
, NULL
);
2302 rc
= CIFSSMBRenameOpenFile(xid
, tcon
, fid
.netfid
,
2303 (const char *) to_dentry
->d_name
.name
,
2304 cifs_sb
->local_nls
, cifs_remap(cifs_sb
));
2305 CIFSSMBClose(xid
, tcon
, fid
.netfid
);
2307 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2310 d_move(from_dentry
, to_dentry
);
2311 cifs_put_tlink(tlink
);
2316 cifs_rename2(struct mnt_idmap
*idmap
, struct inode
*source_dir
,
2317 struct dentry
*source_dentry
, struct inode
*target_dir
,
2318 struct dentry
*target_dentry
, unsigned int flags
)
2320 const char *from_name
, *to_name
;
2321 void *page1
, *page2
;
2322 struct cifs_sb_info
*cifs_sb
;
2323 struct tcon_link
*tlink
;
2324 struct cifs_tcon
*tcon
;
2327 int retry_count
= 0;
2328 FILE_UNIX_BASIC_INFO
*info_buf_source
= NULL
;
2329 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2330 FILE_UNIX_BASIC_INFO
*info_buf_target
;
2331 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2333 if (flags
& ~RENAME_NOREPLACE
)
2336 cifs_sb
= CIFS_SB(source_dir
->i_sb
);
2337 if (unlikely(cifs_forced_shutdown(cifs_sb
)))
2340 tlink
= cifs_sb_tlink(cifs_sb
);
2342 return PTR_ERR(tlink
);
2343 tcon
= tlink_tcon(tlink
);
2345 page1
= alloc_dentry_path();
2346 page2
= alloc_dentry_path();
2349 from_name
= build_path_from_dentry(source_dentry
, page1
);
2350 if (IS_ERR(from_name
)) {
2351 rc
= PTR_ERR(from_name
);
2352 goto cifs_rename_exit
;
2355 to_name
= build_path_from_dentry(target_dentry
, page2
);
2356 if (IS_ERR(to_name
)) {
2357 rc
= PTR_ERR(to_name
);
2358 goto cifs_rename_exit
;
2361 cifs_close_deferred_file_under_dentry(tcon
, from_name
);
2362 if (d_inode(target_dentry
) != NULL
)
2363 cifs_close_deferred_file_under_dentry(tcon
, to_name
);
2365 rc
= cifs_do_rename(xid
, source_dentry
, from_name
, target_dentry
,
2368 if (rc
== -EACCES
) {
2369 while (retry_count
< 3) {
2370 cifs_close_all_deferred_files(tcon
);
2371 rc
= cifs_do_rename(xid
, source_dentry
, from_name
, target_dentry
,
2380 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2382 if (flags
& RENAME_NOREPLACE
)
2383 goto cifs_rename_exit
;
2385 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2386 if (rc
== -EEXIST
&& tcon
->unix_ext
) {
2388 * Are src and dst hardlinks of same inode? We can only tell
2389 * with unix extensions enabled.
2392 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO
),
2394 if (info_buf_source
== NULL
) {
2396 goto cifs_rename_exit
;
2399 info_buf_target
= info_buf_source
+ 1;
2400 tmprc
= CIFSSMBUnixQPathInfo(xid
, tcon
, from_name
,
2403 cifs_remap(cifs_sb
));
2407 tmprc
= CIFSSMBUnixQPathInfo(xid
, tcon
, to_name
,
2410 cifs_remap(cifs_sb
));
2412 if (tmprc
== 0 && (info_buf_source
->UniqueId
==
2413 info_buf_target
->UniqueId
)) {
2414 /* same file, POSIX says that this is a noop */
2416 goto cifs_rename_exit
;
2420 * else ... BB we could add the same check for Windows by
2421 * checking the UniqueId via FILE_INTERNAL_INFO
2425 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2427 /* Try unlinking the target dentry if it's not negative */
2428 if (d_really_is_positive(target_dentry
) && (rc
== -EACCES
|| rc
== -EEXIST
)) {
2429 if (d_is_dir(target_dentry
))
2430 tmprc
= cifs_rmdir(target_dir
, target_dentry
);
2432 tmprc
= cifs_unlink(target_dir
, target_dentry
);
2434 goto cifs_rename_exit
;
2435 rc
= cifs_do_rename(xid
, source_dentry
, from_name
,
2436 target_dentry
, to_name
);
2439 /* force revalidate to go get info when needed */
2440 CIFS_I(source_dir
)->time
= CIFS_I(target_dir
)->time
= 0;
2443 kfree(info_buf_source
);
2444 free_dentry_path(page2
);
2445 free_dentry_path(page1
);
2447 cifs_put_tlink(tlink
);
2452 cifs_dentry_needs_reval(struct dentry
*dentry
)
2454 struct inode
*inode
= d_inode(dentry
);
2455 struct cifsInodeInfo
*cifs_i
= CIFS_I(inode
);
2456 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
2457 struct cifs_tcon
*tcon
= cifs_sb_master_tcon(cifs_sb
);
2458 struct cached_fid
*cfid
= NULL
;
2460 if (cifs_i
->time
== 0)
2463 if (CIFS_CACHE_READ(cifs_i
))
2466 if (!lookupCacheEnabled
)
2469 if (!open_cached_dir_by_dentry(tcon
, dentry
->d_parent
, &cfid
)) {
2470 spin_lock(&cfid
->fid_lock
);
2471 if (cfid
->time
&& cifs_i
->time
> cfid
->time
) {
2472 spin_unlock(&cfid
->fid_lock
);
2473 close_cached_dir(cfid
);
2476 spin_unlock(&cfid
->fid_lock
);
2477 close_cached_dir(cfid
);
2480 * depending on inode type, check if attribute caching disabled for
2481 * files or directories
2483 if (S_ISDIR(inode
->i_mode
)) {
2484 if (!cifs_sb
->ctx
->acdirmax
)
2486 if (!time_in_range(jiffies
, cifs_i
->time
,
2487 cifs_i
->time
+ cifs_sb
->ctx
->acdirmax
))
2490 if (!cifs_sb
->ctx
->acregmax
)
2492 if (!time_in_range(jiffies
, cifs_i
->time
,
2493 cifs_i
->time
+ cifs_sb
->ctx
->acregmax
))
2497 /* hardlinked files w/ noserverino get "special" treatment */
2498 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
) &&
2499 S_ISREG(inode
->i_mode
) && inode
->i_nlink
!= 1)
2506 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2508 * @key: currently unused
2509 * @mode: the task state to sleep in
2512 cifs_wait_bit_killable(struct wait_bit_key
*key
, int mode
)
2515 if (signal_pending_state(mode
, current
))
2516 return -ERESTARTSYS
;
2521 cifs_revalidate_mapping(struct inode
*inode
)
2524 struct cifsInodeInfo
*cifs_inode
= CIFS_I(inode
);
2525 unsigned long *flags
= &cifs_inode
->flags
;
2526 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
2528 /* swapfiles are not supposed to be shared */
2529 if (IS_SWAPFILE(inode
))
2532 rc
= wait_on_bit_lock_action(flags
, CIFS_INO_LOCK
, cifs_wait_bit_killable
,
2533 TASK_KILLABLE
|TASK_FREEZABLE_UNSAFE
);
2537 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING
, flags
)) {
2538 /* for cache=singleclient, do not invalidate */
2539 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_RW_CACHE
)
2540 goto skip_invalidate
;
2542 cifs_inode
->netfs
.zero_point
= cifs_inode
->netfs
.remote_i_size
;
2543 rc
= filemap_invalidate_inode(inode
, true, 0, LLONG_MAX
);
2545 cifs_dbg(VFS
, "%s: invalidate inode %p failed with rc %d\n",
2546 __func__
, inode
, rc
);
2547 set_bit(CIFS_INO_INVALID_MAPPING
, flags
);
2552 clear_bit_unlock(CIFS_INO_LOCK
, flags
);
2553 smp_mb__after_atomic();
2554 wake_up_bit(flags
, CIFS_INO_LOCK
);
2560 cifs_zap_mapping(struct inode
*inode
)
2562 set_bit(CIFS_INO_INVALID_MAPPING
, &CIFS_I(inode
)->flags
);
2563 return cifs_revalidate_mapping(inode
);
2566 int cifs_revalidate_file_attr(struct file
*filp
)
2569 struct dentry
*dentry
= file_dentry(filp
);
2570 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2571 struct cifsFileInfo
*cfile
= (struct cifsFileInfo
*) filp
->private_data
;
2572 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2574 if (!cifs_dentry_needs_reval(dentry
))
2577 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2578 if (tlink_tcon(cfile
->tlink
)->unix_ext
)
2579 rc
= cifs_get_file_info_unix(filp
);
2581 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2582 rc
= cifs_get_file_info(filp
);
2587 int cifs_revalidate_dentry_attr(struct dentry
*dentry
)
2591 struct inode
*inode
= d_inode(dentry
);
2592 struct super_block
*sb
= dentry
->d_sb
;
2593 const char *full_path
;
2600 if (!cifs_dentry_needs_reval(dentry
))
2605 page
= alloc_dentry_path();
2606 full_path
= build_path_from_dentry(dentry
, page
);
2607 if (IS_ERR(full_path
)) {
2608 rc
= PTR_ERR(full_path
);
2612 cifs_dbg(FYI
, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2613 full_path
, inode
, inode
->i_count
.counter
,
2614 dentry
, cifs_get_time(dentry
), jiffies
);
2617 if (cifs_sb_master_tcon(CIFS_SB(sb
))->posix_extensions
) {
2618 rc
= smb311_posix_get_inode_info(&inode
, full_path
,
2620 } else if (cifs_sb_master_tcon(CIFS_SB(sb
))->unix_ext
) {
2621 rc
= cifs_get_inode_info_unix(&inode
, full_path
, sb
, xid
);
2623 rc
= cifs_get_inode_info(&inode
, full_path
, NULL
, sb
,
2626 if (rc
== -EAGAIN
&& count
++ < 10)
2629 free_dentry_path(page
);
2635 int cifs_revalidate_file(struct file
*filp
)
2638 struct inode
*inode
= file_inode(filp
);
2640 rc
= cifs_revalidate_file_attr(filp
);
2644 return cifs_revalidate_mapping(inode
);
2647 /* revalidate a dentry's inode attributes */
2648 int cifs_revalidate_dentry(struct dentry
*dentry
)
2651 struct inode
*inode
= d_inode(dentry
);
2653 rc
= cifs_revalidate_dentry_attr(dentry
);
2657 return cifs_revalidate_mapping(inode
);
2660 int cifs_getattr(struct mnt_idmap
*idmap
, const struct path
*path
,
2661 struct kstat
*stat
, u32 request_mask
, unsigned int flags
)
2663 struct dentry
*dentry
= path
->dentry
;
2664 struct cifs_sb_info
*cifs_sb
= CIFS_SB(dentry
->d_sb
);
2665 struct cifs_tcon
*tcon
= cifs_sb_master_tcon(cifs_sb
);
2666 struct inode
*inode
= d_inode(dentry
);
2669 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode
->i_sb
))))
2673 * We need to be sure that all dirty pages are written and the server
2674 * has actual ctime, mtime and file length.
2676 if ((request_mask
& (STATX_CTIME
| STATX_MTIME
| STATX_SIZE
| STATX_BLOCKS
)) &&
2677 !CIFS_CACHE_READ(CIFS_I(inode
)) &&
2678 inode
->i_mapping
&& inode
->i_mapping
->nrpages
!= 0) {
2679 rc
= filemap_fdatawait(inode
->i_mapping
);
2681 mapping_set_error(inode
->i_mapping
, rc
);
2686 if ((flags
& AT_STATX_SYNC_TYPE
) == AT_STATX_FORCE_SYNC
)
2687 CIFS_I(inode
)->time
= 0; /* force revalidate */
2690 * If the caller doesn't require syncing, only sync if
2691 * necessary (e.g. due to earlier truncate or setattr
2692 * invalidating the cached metadata)
2694 if (((flags
& AT_STATX_SYNC_TYPE
) != AT_STATX_DONT_SYNC
) ||
2695 (CIFS_I(inode
)->time
== 0)) {
2696 rc
= cifs_revalidate_dentry_attr(dentry
);
2701 generic_fillattr(&nop_mnt_idmap
, request_mask
, inode
, stat
);
2702 stat
->blksize
= cifs_sb
->ctx
->bsize
;
2703 stat
->ino
= CIFS_I(inode
)->uniqueid
;
2705 /* old CIFS Unix Extensions doesn't return create time */
2706 if (CIFS_I(inode
)->createtime
) {
2707 stat
->result_mask
|= STATX_BTIME
;
2709 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode
)->createtime
));
2712 stat
->attributes_mask
|= (STATX_ATTR_COMPRESSED
| STATX_ATTR_ENCRYPTED
);
2713 if (CIFS_I(inode
)->cifsAttrs
& FILE_ATTRIBUTE_COMPRESSED
)
2714 stat
->attributes
|= STATX_ATTR_COMPRESSED
;
2715 if (CIFS_I(inode
)->cifsAttrs
& FILE_ATTRIBUTE_ENCRYPTED
)
2716 stat
->attributes
|= STATX_ATTR_ENCRYPTED
;
2719 * If on a multiuser mount without unix extensions or cifsacl being
2720 * enabled, and the admin hasn't overridden them, set the ownership
2721 * to the fsuid/fsgid of the current process.
2723 if ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MULTIUSER
) &&
2724 !(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_CIFS_ACL
) &&
2726 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_OVERR_UID
))
2727 stat
->uid
= current_fsuid();
2728 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_OVERR_GID
))
2729 stat
->gid
= current_fsgid();
2734 int cifs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fei
, u64 start
,
2737 struct cifsInodeInfo
*cifs_i
= CIFS_I(inode
);
2738 struct cifs_sb_info
*cifs_sb
= CIFS_SB(cifs_i
->netfs
.inode
.i_sb
);
2739 struct cifs_tcon
*tcon
= cifs_sb_master_tcon(cifs_sb
);
2740 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2741 struct cifsFileInfo
*cfile
;
2744 if (unlikely(cifs_forced_shutdown(cifs_sb
)))
2748 * We need to be sure that all dirty pages are written as they
2749 * might fill holes on the server.
2751 if (!CIFS_CACHE_READ(CIFS_I(inode
)) && inode
->i_mapping
&&
2752 inode
->i_mapping
->nrpages
!= 0) {
2753 rc
= filemap_fdatawait(inode
->i_mapping
);
2755 mapping_set_error(inode
->i_mapping
, rc
);
2760 cfile
= find_readable_file(cifs_i
, false);
2764 if (server
->ops
->fiemap
) {
2765 rc
= server
->ops
->fiemap(tcon
, cfile
, fei
, start
, len
);
2766 cifsFileInfo_put(cfile
);
2770 cifsFileInfo_put(cfile
);
2774 int cifs_truncate_page(struct address_space
*mapping
, loff_t from
)
2776 pgoff_t index
= from
>> PAGE_SHIFT
;
2777 unsigned offset
= from
& (PAGE_SIZE
- 1);
2781 page
= grab_cache_page(mapping
, index
);
2785 zero_user_segment(page
, offset
, PAGE_SIZE
);
2791 void cifs_setsize(struct inode
*inode
, loff_t offset
)
2793 struct cifsInodeInfo
*cifs_i
= CIFS_I(inode
);
2795 spin_lock(&inode
->i_lock
);
2796 i_size_write(inode
, offset
);
2797 spin_unlock(&inode
->i_lock
);
2799 /* Cached inode must be refreshed on truncate */
2801 truncate_pagecache(inode
, offset
);
2805 cifs_set_file_size(struct inode
*inode
, struct iattr
*attrs
,
2806 unsigned int xid
, const char *full_path
, struct dentry
*dentry
)
2809 struct cifsFileInfo
*open_file
;
2810 struct cifsInodeInfo
*cifsInode
= CIFS_I(inode
);
2811 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
2812 struct tcon_link
*tlink
= NULL
;
2813 struct cifs_tcon
*tcon
= NULL
;
2814 struct TCP_Server_Info
*server
;
2817 * To avoid spurious oplock breaks from server, in the case of
2818 * inodes that we already have open, avoid doing path based
2819 * setting of file size if we can do it by handle.
2820 * This keeps our caching token (oplock) and avoids timeouts
2821 * when the local oplock break takes longer to flush
2822 * writebehind data than the SMB timeout for the SetPathInfo
2823 * request would allow
2825 open_file
= find_writable_file(cifsInode
, FIND_WR_FSUID_ONLY
);
2827 tcon
= tlink_tcon(open_file
->tlink
);
2828 server
= tcon
->ses
->server
;
2829 if (server
->ops
->set_file_size
)
2830 rc
= server
->ops
->set_file_size(xid
, tcon
, open_file
,
2831 attrs
->ia_size
, false);
2834 cifsFileInfo_put(open_file
);
2835 cifs_dbg(FYI
, "SetFSize for attrs rc = %d\n", rc
);
2843 tlink
= cifs_sb_tlink(cifs_sb
);
2845 return PTR_ERR(tlink
);
2846 tcon
= tlink_tcon(tlink
);
2847 server
= tcon
->ses
->server
;
2851 * Set file size by pathname rather than by handle either because no
2852 * valid, writeable file handle for it was found or because there was
2853 * an error setting it by handle.
2855 if (server
->ops
->set_path_size
)
2856 rc
= server
->ops
->set_path_size(xid
, tcon
, full_path
,
2857 attrs
->ia_size
, cifs_sb
, false, dentry
);
2860 cifs_dbg(FYI
, "SetEOF by path (setattrs) rc = %d\n", rc
);
2863 cifs_put_tlink(tlink
);
2867 netfs_resize_file(&cifsInode
->netfs
, attrs
->ia_size
, true);
2868 cifs_setsize(inode
, attrs
->ia_size
);
2870 * i_blocks is not related to (i_size / i_blksize), but instead
2871 * 512 byte (2**9) size is required for calculating num blocks.
2872 * Until we can query the server for actual allocation size,
2873 * this is best estimate we have for blocks allocated for a file
2874 * Number of blocks must be rounded up so size 1 is not 0 blocks
2876 inode
->i_blocks
= (512 - 1 + attrs
->ia_size
) >> 9;
2879 * The man page of truncate says if the size changed,
2880 * then the st_ctime and st_mtime fields for the file
2883 attrs
->ia_ctime
= attrs
->ia_mtime
= current_time(inode
);
2884 attrs
->ia_valid
|= ATTR_CTIME
| ATTR_MTIME
;
2886 cifs_truncate_page(inode
->i_mapping
, inode
->i_size
);
2892 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2894 cifs_setattr_unix(struct dentry
*direntry
, struct iattr
*attrs
)
2898 const char *full_path
;
2899 void *page
= alloc_dentry_path();
2900 struct inode
*inode
= d_inode(direntry
);
2901 struct cifsInodeInfo
*cifsInode
= CIFS_I(inode
);
2902 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
2903 struct tcon_link
*tlink
;
2904 struct cifs_tcon
*pTcon
;
2905 struct cifs_unix_set_info_args
*args
= NULL
;
2906 struct cifsFileInfo
*open_file
;
2908 cifs_dbg(FYI
, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2909 direntry
, attrs
->ia_valid
);
2913 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_PERM
)
2914 attrs
->ia_valid
|= ATTR_FORCE
;
2916 rc
= setattr_prepare(&nop_mnt_idmap
, direntry
, attrs
);
2920 full_path
= build_path_from_dentry(direntry
, page
);
2921 if (IS_ERR(full_path
)) {
2922 rc
= PTR_ERR(full_path
);
2927 * Attempt to flush data before changing attributes. We need to do
2928 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2929 * ownership or mode then we may also need to do this. Here, we take
2930 * the safe way out and just do the flush on all setattr requests. If
2931 * the flush returns error, store it to report later and continue.
2933 * BB: This should be smarter. Why bother flushing pages that
2934 * will be truncated anyway? Also, should we error out here if
2935 * the flush returns error?
2937 rc
= filemap_write_and_wait(inode
->i_mapping
);
2938 if (is_interrupt_error(rc
)) {
2943 mapping_set_error(inode
->i_mapping
, rc
);
2946 if (attrs
->ia_valid
& ATTR_SIZE
) {
2947 rc
= cifs_set_file_size(inode
, attrs
, xid
, full_path
, direntry
);
2952 /* skip mode change if it's just for clearing setuid/setgid */
2953 if (attrs
->ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
))
2954 attrs
->ia_valid
&= ~ATTR_MODE
;
2956 args
= kmalloc(sizeof(*args
), GFP_KERNEL
);
2962 /* set up the struct */
2963 if (attrs
->ia_valid
& ATTR_MODE
)
2964 args
->mode
= attrs
->ia_mode
;
2966 args
->mode
= NO_CHANGE_64
;
2968 if (attrs
->ia_valid
& ATTR_UID
)
2969 args
->uid
= attrs
->ia_uid
;
2971 args
->uid
= INVALID_UID
; /* no change */
2973 if (attrs
->ia_valid
& ATTR_GID
)
2974 args
->gid
= attrs
->ia_gid
;
2976 args
->gid
= INVALID_GID
; /* no change */
2978 if (attrs
->ia_valid
& ATTR_ATIME
)
2979 args
->atime
= cifs_UnixTimeToNT(attrs
->ia_atime
);
2981 args
->atime
= NO_CHANGE_64
;
2983 if (attrs
->ia_valid
& ATTR_MTIME
)
2984 args
->mtime
= cifs_UnixTimeToNT(attrs
->ia_mtime
);
2986 args
->mtime
= NO_CHANGE_64
;
2988 if (attrs
->ia_valid
& ATTR_CTIME
)
2989 args
->ctime
= cifs_UnixTimeToNT(attrs
->ia_ctime
);
2991 args
->ctime
= NO_CHANGE_64
;
2994 open_file
= find_writable_file(cifsInode
, FIND_WR_FSUID_ONLY
);
2996 u16 nfid
= open_file
->fid
.netfid
;
2997 u32 npid
= open_file
->pid
;
2998 pTcon
= tlink_tcon(open_file
->tlink
);
2999 rc
= CIFSSMBUnixSetFileInfo(xid
, pTcon
, args
, nfid
, npid
);
3000 cifsFileInfo_put(open_file
);
3002 tlink
= cifs_sb_tlink(cifs_sb
);
3003 if (IS_ERR(tlink
)) {
3004 rc
= PTR_ERR(tlink
);
3007 pTcon
= tlink_tcon(tlink
);
3008 rc
= CIFSSMBUnixSetPathInfo(xid
, pTcon
, full_path
, args
,
3010 cifs_remap(cifs_sb
));
3011 cifs_put_tlink(tlink
);
3017 if ((attrs
->ia_valid
& ATTR_SIZE
) &&
3018 attrs
->ia_size
!= i_size_read(inode
)) {
3019 truncate_setsize(inode
, attrs
->ia_size
);
3020 netfs_resize_file(&cifsInode
->netfs
, attrs
->ia_size
, true);
3021 fscache_resize_cookie(cifs_inode_cookie(inode
), attrs
->ia_size
);
3024 setattr_copy(&nop_mnt_idmap
, inode
, attrs
);
3025 mark_inode_dirty(inode
);
3027 /* force revalidate when any of these times are set since some
3028 of the fs types (eg ext3, fat) do not have fine enough
3029 time granularity to match protocol, and we do not have a
3030 a way (yet) to query the server fs's time granularity (and
3031 whether it rounds times down).
3033 if (attrs
->ia_valid
& (ATTR_MTIME
| ATTR_CTIME
))
3034 cifsInode
->time
= 0;
3037 free_dentry_path(page
);
3041 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3044 cifs_setattr_nounix(struct dentry
*direntry
, struct iattr
*attrs
)
3047 kuid_t uid
= INVALID_UID
;
3048 kgid_t gid
= INVALID_GID
;
3049 struct inode
*inode
= d_inode(direntry
);
3050 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
3051 struct cifsInodeInfo
*cifsInode
= CIFS_I(inode
);
3052 struct cifsFileInfo
*wfile
;
3053 struct cifs_tcon
*tcon
;
3054 const char *full_path
;
3055 void *page
= alloc_dentry_path();
3058 __u64 mode
= NO_CHANGE_64
;
3062 cifs_dbg(FYI
, "setattr on file %pd attrs->ia_valid 0x%x\n",
3063 direntry
, attrs
->ia_valid
);
3065 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_PERM
)
3066 attrs
->ia_valid
|= ATTR_FORCE
;
3068 rc
= setattr_prepare(&nop_mnt_idmap
, direntry
, attrs
);
3070 goto cifs_setattr_exit
;
3072 full_path
= build_path_from_dentry(direntry
, page
);
3073 if (IS_ERR(full_path
)) {
3074 rc
= PTR_ERR(full_path
);
3075 goto cifs_setattr_exit
;
3079 * Attempt to flush data before changing attributes. We need to do
3080 * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
3081 * returns error, store it to report later and continue.
3083 * BB: This should be smarter. Why bother flushing pages that
3084 * will be truncated anyway? Also, should we error out here if
3085 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
3087 if (attrs
->ia_valid
& (ATTR_MTIME
| ATTR_SIZE
| ATTR_CTIME
)) {
3088 rc
= filemap_write_and_wait(inode
->i_mapping
);
3089 if (is_interrupt_error(rc
)) {
3091 goto cifs_setattr_exit
;
3093 mapping_set_error(inode
->i_mapping
, rc
);
3098 if ((attrs
->ia_valid
& ATTR_MTIME
) &&
3099 !(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NOSSYNC
)) {
3100 rc
= cifs_get_writable_file(cifsInode
, FIND_WR_ANY
, &wfile
);
3102 tcon
= tlink_tcon(wfile
->tlink
);
3103 rc
= tcon
->ses
->server
->ops
->flush(xid
, tcon
, &wfile
->fid
);
3104 cifsFileInfo_put(wfile
);
3106 goto cifs_setattr_exit
;
3107 } else if (rc
!= -EBADF
)
3108 goto cifs_setattr_exit
;
3113 if (attrs
->ia_valid
& ATTR_SIZE
) {
3114 rc
= cifs_set_file_size(inode
, attrs
, xid
, full_path
, direntry
);
3116 goto cifs_setattr_exit
;
3119 if (attrs
->ia_valid
& ATTR_UID
)
3120 uid
= attrs
->ia_uid
;
3122 if (attrs
->ia_valid
& ATTR_GID
)
3123 gid
= attrs
->ia_gid
;
3125 if ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_CIFS_ACL
) ||
3126 (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MODE_FROM_SID
)) {
3127 if (uid_valid(uid
) || gid_valid(gid
)) {
3128 mode
= NO_CHANGE_64
;
3129 rc
= id_mode_to_cifs_acl(inode
, full_path
, &mode
,
3132 cifs_dbg(FYI
, "%s: Setting id failed with error: %d\n",
3134 goto cifs_setattr_exit
;
3138 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SET_UID
))
3139 attrs
->ia_valid
&= ~(ATTR_UID
| ATTR_GID
);
3141 /* skip mode change if it's just for clearing setuid/setgid */
3142 if (attrs
->ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
))
3143 attrs
->ia_valid
&= ~ATTR_MODE
;
3145 if (attrs
->ia_valid
& ATTR_MODE
) {
3146 mode
= attrs
->ia_mode
;
3148 if ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_CIFS_ACL
) ||
3149 (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MODE_FROM_SID
)) {
3150 rc
= id_mode_to_cifs_acl(inode
, full_path
, &mode
,
3151 INVALID_UID
, INVALID_GID
);
3153 cifs_dbg(FYI
, "%s: Setting ACL failed with error: %d\n",
3155 goto cifs_setattr_exit
;
3159 * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
3160 * Pick up the actual mode bits that were set.
3162 if (mode
!= attrs
->ia_mode
)
3163 attrs
->ia_mode
= mode
;
3165 if (((mode
& S_IWUGO
) == 0) &&
3166 (cifsInode
->cifsAttrs
& ATTR_READONLY
) == 0) {
3168 dosattr
= cifsInode
->cifsAttrs
| ATTR_READONLY
;
3170 /* fix up mode if we're not using dynperm */
3171 if ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_DYNPERM
) == 0)
3172 attrs
->ia_mode
= inode
->i_mode
& ~S_IWUGO
;
3173 } else if ((mode
& S_IWUGO
) &&
3174 (cifsInode
->cifsAttrs
& ATTR_READONLY
)) {
3176 dosattr
= cifsInode
->cifsAttrs
& ~ATTR_READONLY
;
3177 /* Attributes of 0 are ignored */
3179 dosattr
|= ATTR_NORMAL
;
3181 /* reset local inode permissions to normal */
3182 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_DYNPERM
)) {
3183 attrs
->ia_mode
&= ~(S_IALLUGO
);
3184 if (S_ISDIR(inode
->i_mode
))
3186 cifs_sb
->ctx
->dir_mode
;
3189 cifs_sb
->ctx
->file_mode
;
3191 } else if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_DYNPERM
)) {
3192 /* ignore mode change - ATTR_READONLY hasn't changed */
3193 attrs
->ia_valid
&= ~ATTR_MODE
;
3197 if (attrs
->ia_valid
& (ATTR_MTIME
|ATTR_ATIME
|ATTR_CTIME
) ||
3198 ((attrs
->ia_valid
& ATTR_MODE
) && dosattr
)) {
3199 rc
= cifs_set_file_info(inode
, attrs
, xid
, full_path
, dosattr
);
3200 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3202 /* Even if error on time set, no sense failing the call if
3203 the server would set the time to a reasonable value anyway,
3204 and this check ensures that we are not being called from
3205 sys_utimes in which case we ought to fail the call back to
3206 the user when the server rejects the call */
3207 if ((rc
) && (attrs
->ia_valid
&
3208 (ATTR_MODE
| ATTR_GID
| ATTR_UID
| ATTR_SIZE
)))
3212 /* do not need local check to inode_check_ok since the server does
3215 goto cifs_setattr_exit
;
3217 if ((attrs
->ia_valid
& ATTR_SIZE
) &&
3218 attrs
->ia_size
!= i_size_read(inode
)) {
3219 truncate_setsize(inode
, attrs
->ia_size
);
3220 netfs_resize_file(&cifsInode
->netfs
, attrs
->ia_size
, true);
3221 fscache_resize_cookie(cifs_inode_cookie(inode
), attrs
->ia_size
);
3224 setattr_copy(&nop_mnt_idmap
, inode
, attrs
);
3225 mark_inode_dirty(inode
);
3229 free_dentry_path(page
);
3234 cifs_setattr(struct mnt_idmap
*idmap
, struct dentry
*direntry
,
3235 struct iattr
*attrs
)
3237 struct cifs_sb_info
*cifs_sb
= CIFS_SB(direntry
->d_sb
);
3238 int rc
, retries
= 0;
3239 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3240 struct cifs_tcon
*pTcon
= cifs_sb_master_tcon(cifs_sb
);
3241 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3243 if (unlikely(cifs_forced_shutdown(cifs_sb
)))
3247 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3248 if (pTcon
->unix_ext
)
3249 rc
= cifs_setattr_unix(direntry
, attrs
);
3251 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3252 rc
= cifs_setattr_nounix(direntry
, attrs
);
3254 } while (is_retryable_error(rc
) && retries
< 2);
3256 /* BB: add cifs_setattr_legacy for really old servers */