1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 /* 2 /* 3 * linux/fs/stat.c 3 * linux/fs/stat.c 4 * 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 */ 6 */ 7 7 8 #include <linux/blkdev.h> 8 #include <linux/blkdev.h> 9 #include <linux/export.h> 9 #include <linux/export.h> 10 #include <linux/mm.h> 10 #include <linux/mm.h> 11 #include <linux/errno.h> 11 #include <linux/errno.h> 12 #include <linux/file.h> 12 #include <linux/file.h> 13 #include <linux/highuid.h> 13 #include <linux/highuid.h> 14 #include <linux/fs.h> 14 #include <linux/fs.h> 15 #include <linux/namei.h> 15 #include <linux/namei.h> 16 #include <linux/security.h> 16 #include <linux/security.h> 17 #include <linux/cred.h> 17 #include <linux/cred.h> 18 #include <linux/syscalls.h> 18 #include <linux/syscalls.h> 19 #include <linux/pagemap.h> 19 #include <linux/pagemap.h> 20 #include <linux/compat.h> 20 #include <linux/compat.h> 21 #include <linux/iversion.h> 21 #include <linux/iversion.h> 22 22 23 #include <linux/uaccess.h> 23 #include <linux/uaccess.h> 24 #include <asm/unistd.h> 24 #include <asm/unistd.h> 25 25 26 #include "internal.h" 26 #include "internal.h" 27 #include "mount.h" 27 #include "mount.h" 28 28 29 /** 29 /** 30 * generic_fillattr - Fill in the basic attrib 30 * generic_fillattr - Fill in the basic attributes from the inode struct 31 * @idmap: idmap of the mount the !! 31 * @idmap: idmap of the mount the inode was found from 32 * @request_mask: statx request_mask !! 32 * @inode: Inode to use as the source 33 * @inode: Inode to use as the so !! 33 * @stat: Where to fill in the attributes 34 * @stat: Where to fill in the a << 35 * 34 * 36 * Fill in the basic attributes in the kstat s 35 * Fill in the basic attributes in the kstat structure from data that's to be 37 * found on the VFS inode structure. This is 36 * found on the VFS inode structure. This is the default if no getattr inode 38 * operation is supplied. 37 * operation is supplied. 39 * 38 * 40 * If the inode has been found through an idma 39 * If the inode has been found through an idmapped mount the idmap of 41 * the vfsmount must be passed through @idmap. 40 * the vfsmount must be passed through @idmap. This function will then 42 * take care to map the inode according to @id 41 * take care to map the inode according to @idmap before filling in the 43 * uid and gid filds. On non-idmapped mounts o 42 * uid and gid filds. On non-idmapped mounts or if permission checking is to be 44 * performed on the raw inode simply pass @nop !! 43 * performed on the raw inode simply passs @nop_mnt_idmap. 45 */ 44 */ 46 void generic_fillattr(struct mnt_idmap *idmap, !! 45 void generic_fillattr(struct mnt_idmap *idmap, struct inode *inode, 47 struct inode *inode, str !! 46 struct kstat *stat) 48 { 47 { 49 vfsuid_t vfsuid = i_uid_into_vfsuid(id 48 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); 50 vfsgid_t vfsgid = i_gid_into_vfsgid(id 49 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); 51 50 52 stat->dev = inode->i_sb->s_dev; 51 stat->dev = inode->i_sb->s_dev; 53 stat->ino = inode->i_ino; 52 stat->ino = inode->i_ino; 54 stat->mode = inode->i_mode; 53 stat->mode = inode->i_mode; 55 stat->nlink = inode->i_nlink; 54 stat->nlink = inode->i_nlink; 56 stat->uid = vfsuid_into_kuid(vfsuid); 55 stat->uid = vfsuid_into_kuid(vfsuid); 57 stat->gid = vfsgid_into_kgid(vfsgid); 56 stat->gid = vfsgid_into_kgid(vfsgid); 58 stat->rdev = inode->i_rdev; 57 stat->rdev = inode->i_rdev; 59 stat->size = i_size_read(inode); 58 stat->size = i_size_read(inode); 60 stat->atime = inode_get_atime(inode); !! 59 stat->atime = inode->i_atime; 61 stat->mtime = inode_get_mtime(inode); !! 60 stat->mtime = inode->i_mtime; 62 stat->ctime = inode_get_ctime(inode); !! 61 stat->ctime = inode->i_ctime; 63 stat->blksize = i_blocksize(inode); 62 stat->blksize = i_blocksize(inode); 64 stat->blocks = inode->i_blocks; 63 stat->blocks = inode->i_blocks; 65 << 66 if ((request_mask & STATX_CHANGE_COOKI << 67 stat->result_mask |= STATX_CHA << 68 stat->change_cookie = inode_qu << 69 } << 70 << 71 } 64 } 72 EXPORT_SYMBOL(generic_fillattr); 65 EXPORT_SYMBOL(generic_fillattr); 73 66 74 /** 67 /** 75 * generic_fill_statx_attr - Fill in the statx 68 * generic_fill_statx_attr - Fill in the statx attributes from the inode flags 76 * @inode: Inode to use as the source 69 * @inode: Inode to use as the source 77 * @stat: Where to fill in the attribute 70 * @stat: Where to fill in the attribute flags 78 * 71 * 79 * Fill in the STATX_ATTR_* flags in the kstat 72 * Fill in the STATX_ATTR_* flags in the kstat structure for properties of the 80 * inode that are published on i_flags and enf 73 * inode that are published on i_flags and enforced by the VFS. 81 */ 74 */ 82 void generic_fill_statx_attr(struct inode *ino 75 void generic_fill_statx_attr(struct inode *inode, struct kstat *stat) 83 { 76 { 84 if (inode->i_flags & S_IMMUTABLE) 77 if (inode->i_flags & S_IMMUTABLE) 85 stat->attributes |= STATX_ATTR 78 stat->attributes |= STATX_ATTR_IMMUTABLE; 86 if (inode->i_flags & S_APPEND) 79 if (inode->i_flags & S_APPEND) 87 stat->attributes |= STATX_ATTR 80 stat->attributes |= STATX_ATTR_APPEND; 88 stat->attributes_mask |= KSTAT_ATTR_VF 81 stat->attributes_mask |= KSTAT_ATTR_VFS_FLAGS; 89 } 82 } 90 EXPORT_SYMBOL(generic_fill_statx_attr); 83 EXPORT_SYMBOL(generic_fill_statx_attr); 91 84 92 /** 85 /** 93 * generic_fill_statx_atomic_writes - Fill in << 94 * @stat: Where to fill in the attribute << 95 * @unit_min: Minimum supported atomic write << 96 * @unit_max: Maximum supported atomic write << 97 * << 98 * Fill in the STATX{_ATTR}_WRITE_ATOMIC flags << 99 * atomic write unit_min and unit_max values. << 100 */ << 101 void generic_fill_statx_atomic_writes(struct k << 102 unsigned << 103 unsigned << 104 { << 105 /* Confirm that the request type is kn << 106 stat->result_mask |= STATX_WRITE_ATOMI << 107 << 108 /* Confirm that the file attribute typ << 109 stat->attributes_mask |= STATX_ATTR_WR << 110 << 111 if (unit_min) { << 112 stat->atomic_write_unit_min = << 113 stat->atomic_write_unit_max = << 114 /* Initially only allow 1x seg << 115 stat->atomic_write_segments_ma << 116 << 117 /* Confirm atomic writes are a << 118 stat->attributes |= STATX_ATTR << 119 } << 120 } << 121 EXPORT_SYMBOL_GPL(generic_fill_statx_atomic_wr << 122 << 123 /** << 124 * vfs_getattr_nosec - getattr without securit 86 * vfs_getattr_nosec - getattr without security checks 125 * @path: file to get attributes from 87 * @path: file to get attributes from 126 * @stat: structure to return attributes in 88 * @stat: structure to return attributes in 127 * @request_mask: STATX_xxx flags indicating w 89 * @request_mask: STATX_xxx flags indicating what the caller wants 128 * @query_flags: Query mode (AT_STATX_SYNC_TYP 90 * @query_flags: Query mode (AT_STATX_SYNC_TYPE) 129 * 91 * 130 * Get attributes without calling security_ino 92 * Get attributes without calling security_inode_getattr. 131 * 93 * 132 * Currently the only caller other than vfs_ge 94 * Currently the only caller other than vfs_getattr is internal to the 133 * filehandle lookup code, which uses only the 95 * filehandle lookup code, which uses only the inode number and returns no 134 * attributes to any user. Any other code pro 96 * attributes to any user. Any other code probably wants vfs_getattr. 135 */ 97 */ 136 int vfs_getattr_nosec(const struct path *path, 98 int vfs_getattr_nosec(const struct path *path, struct kstat *stat, 137 u32 request_mask, unsign 99 u32 request_mask, unsigned int query_flags) 138 { 100 { 139 struct mnt_idmap *idmap; 101 struct mnt_idmap *idmap; 140 struct inode *inode = d_backing_inode( 102 struct inode *inode = d_backing_inode(path->dentry); 141 103 142 memset(stat, 0, sizeof(*stat)); 104 memset(stat, 0, sizeof(*stat)); 143 stat->result_mask |= STATX_BASIC_STATS 105 stat->result_mask |= STATX_BASIC_STATS; 144 query_flags &= AT_STATX_SYNC_TYPE; 106 query_flags &= AT_STATX_SYNC_TYPE; 145 107 146 /* allow the fs to override these if i 108 /* allow the fs to override these if it really wants to */ 147 /* SB_NOATIME means filesystem supplie 109 /* SB_NOATIME means filesystem supplies dummy atime value */ 148 if (inode->i_sb->s_flags & SB_NOATIME) 110 if (inode->i_sb->s_flags & SB_NOATIME) 149 stat->result_mask &= ~STATX_AT 111 stat->result_mask &= ~STATX_ATIME; 150 112 151 /* 113 /* 152 * Note: If you add another clause to 114 * Note: If you add another clause to set an attribute flag, please 153 * update attributes_mask below. 115 * update attributes_mask below. 154 */ 116 */ 155 if (IS_AUTOMOUNT(inode)) 117 if (IS_AUTOMOUNT(inode)) 156 stat->attributes |= STATX_ATTR 118 stat->attributes |= STATX_ATTR_AUTOMOUNT; 157 119 158 if (IS_DAX(inode)) 120 if (IS_DAX(inode)) 159 stat->attributes |= STATX_ATTR 121 stat->attributes |= STATX_ATTR_DAX; 160 122 161 stat->attributes_mask |= (STATX_ATTR_A 123 stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT | 162 STATX_ATTR_D 124 STATX_ATTR_DAX); 163 125 >> 126 if ((request_mask & STATX_CHANGE_COOKIE) && IS_I_VERSION(inode)) { >> 127 stat->result_mask |= STATX_CHANGE_COOKIE; >> 128 stat->change_cookie = inode_query_iversion(inode); >> 129 } >> 130 164 idmap = mnt_idmap(path->mnt); 131 idmap = mnt_idmap(path->mnt); 165 if (inode->i_op->getattr) 132 if (inode->i_op->getattr) 166 return inode->i_op->getattr(id 133 return inode->i_op->getattr(idmap, path, stat, 167 re !! 134 request_mask, query_flags); 168 qu << 169 135 170 generic_fillattr(idmap, request_mask, !! 136 generic_fillattr(idmap, inode, stat); 171 return 0; 137 return 0; 172 } 138 } 173 EXPORT_SYMBOL(vfs_getattr_nosec); 139 EXPORT_SYMBOL(vfs_getattr_nosec); 174 140 175 /* 141 /* 176 * vfs_getattr - Get the enhanced basic attrib 142 * vfs_getattr - Get the enhanced basic attributes of a file 177 * @path: The file of interest 143 * @path: The file of interest 178 * @stat: Where to return the statistics 144 * @stat: Where to return the statistics 179 * @request_mask: STATX_xxx flags indicating w 145 * @request_mask: STATX_xxx flags indicating what the caller wants 180 * @query_flags: Query mode (AT_STATX_SYNC_TYP 146 * @query_flags: Query mode (AT_STATX_SYNC_TYPE) 181 * 147 * 182 * Ask the filesystem for a file's attributes. 148 * Ask the filesystem for a file's attributes. The caller must indicate in 183 * request_mask and query_flags to indicate wh 149 * request_mask and query_flags to indicate what they want. 184 * 150 * 185 * If the file is remote, the filesystem can b 151 * If the file is remote, the filesystem can be forced to update the attributes 186 * from the backing store by passing AT_STATX_ 152 * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can 187 * suppress the update by passing AT_STATX_DON 153 * suppress the update by passing AT_STATX_DONT_SYNC. 188 * 154 * 189 * Bits must have been set in request_mask to 155 * Bits must have been set in request_mask to indicate which attributes the 190 * caller wants retrieving. Any such attribut 156 * caller wants retrieving. Any such attribute not requested may be returned 191 * anyway, but the value may be approximate, a 157 * anyway, but the value may be approximate, and, if remote, may not have been 192 * synchronised with the server. 158 * synchronised with the server. 193 * 159 * 194 * 0 will be returned on success, and a -ve er 160 * 0 will be returned on success, and a -ve error code if unsuccessful. 195 */ 161 */ 196 int vfs_getattr(const struct path *path, struc 162 int vfs_getattr(const struct path *path, struct kstat *stat, 197 u32 request_mask, unsigned int 163 u32 request_mask, unsigned int query_flags) 198 { 164 { 199 int retval; 165 int retval; 200 166 201 if (WARN_ON_ONCE(query_flags & AT_GETA << 202 return -EPERM; << 203 << 204 retval = security_inode_getattr(path); 167 retval = security_inode_getattr(path); 205 if (retval) 168 if (retval) 206 return retval; 169 return retval; 207 return vfs_getattr_nosec(path, stat, r 170 return vfs_getattr_nosec(path, stat, request_mask, query_flags); 208 } 171 } 209 EXPORT_SYMBOL(vfs_getattr); 172 EXPORT_SYMBOL(vfs_getattr); 210 173 211 /** 174 /** 212 * vfs_fstat - Get the basic attributes by fil 175 * vfs_fstat - Get the basic attributes by file descriptor 213 * @fd: The file descriptor referring to the f 176 * @fd: The file descriptor referring to the file of interest 214 * @stat: The result structure to fill in. 177 * @stat: The result structure to fill in. 215 * 178 * 216 * This function is a wrapper around vfs_getat 179 * This function is a wrapper around vfs_getattr(). The main difference is 217 * that it uses a file descriptor to determine 180 * that it uses a file descriptor to determine the file location. 218 * 181 * 219 * 0 will be returned on success, and a -ve er 182 * 0 will be returned on success, and a -ve error code if unsuccessful. 220 */ 183 */ 221 int vfs_fstat(int fd, struct kstat *stat) 184 int vfs_fstat(int fd, struct kstat *stat) 222 { 185 { 223 struct fd f; 186 struct fd f; 224 int error; 187 int error; 225 188 226 f = fdget_raw(fd); 189 f = fdget_raw(fd); 227 if (!f.file) 190 if (!f.file) 228 return -EBADF; 191 return -EBADF; 229 error = vfs_getattr(&f.file->f_path, s 192 error = vfs_getattr(&f.file->f_path, stat, STATX_BASIC_STATS, 0); 230 fdput(f); 193 fdput(f); 231 return error; 194 return error; 232 } 195 } 233 196 234 int getname_statx_lookup_flags(int flags) 197 int getname_statx_lookup_flags(int flags) 235 { 198 { 236 int lookup_flags = 0; 199 int lookup_flags = 0; 237 200 238 if (!(flags & AT_SYMLINK_NOFOLLOW)) 201 if (!(flags & AT_SYMLINK_NOFOLLOW)) 239 lookup_flags |= LOOKUP_FOLLOW; 202 lookup_flags |= LOOKUP_FOLLOW; 240 if (!(flags & AT_NO_AUTOMOUNT)) 203 if (!(flags & AT_NO_AUTOMOUNT)) 241 lookup_flags |= LOOKUP_AUTOMOU 204 lookup_flags |= LOOKUP_AUTOMOUNT; 242 if (flags & AT_EMPTY_PATH) 205 if (flags & AT_EMPTY_PATH) 243 lookup_flags |= LOOKUP_EMPTY; 206 lookup_flags |= LOOKUP_EMPTY; 244 207 245 return lookup_flags; 208 return lookup_flags; 246 } 209 } 247 210 248 static int vfs_statx_path(struct path *path, i << 249 u32 request_mask) << 250 { << 251 int error = vfs_getattr(path, stat, re << 252 << 253 if (request_mask & STATX_MNT_ID_UNIQUE << 254 stat->mnt_id = real_mount(path << 255 stat->result_mask |= STATX_MNT << 256 } else { << 257 stat->mnt_id = real_mount(path << 258 stat->result_mask |= STATX_MNT << 259 } << 260 << 261 if (path_mounted(path)) << 262 stat->attributes |= STATX_ATTR << 263 stat->attributes_mask |= STATX_ATTR_MO << 264 << 265 /* << 266 * If this is a block device inode, ov << 267 * attributes with the block device sp << 268 * obtained from the bdev backing inod << 269 */ << 270 if (S_ISBLK(stat->mode)) << 271 bdev_statx(path, stat, request << 272 << 273 return error; << 274 } << 275 << 276 static int vfs_statx_fd(int fd, int flags, str << 277 u32 request_mask) << 278 { << 279 CLASS(fd_raw, f)(fd); << 280 if (!f.file) << 281 return -EBADF; << 282 return vfs_statx_path(&f.file->f_path, << 283 } << 284 << 285 /** 211 /** 286 * vfs_statx - Get basic and extra attributes 212 * vfs_statx - Get basic and extra attributes by filename 287 * @dfd: A file descriptor representing the ba 213 * @dfd: A file descriptor representing the base dir for a relative filename 288 * @filename: The name of the file of interest 214 * @filename: The name of the file of interest 289 * @flags: Flags to control the query 215 * @flags: Flags to control the query 290 * @stat: The result structure to fill in. 216 * @stat: The result structure to fill in. 291 * @request_mask: STATX_xxx flags indicating w 217 * @request_mask: STATX_xxx flags indicating what the caller wants 292 * 218 * 293 * This function is a wrapper around vfs_getat 219 * This function is a wrapper around vfs_getattr(). The main difference is 294 * that it uses a filename and base directory 220 * that it uses a filename and base directory to determine the file location. 295 * Additionally, the use of AT_SYMLINK_NOFOLLO 221 * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink 296 * at the given name from being referenced. 222 * at the given name from being referenced. 297 * 223 * 298 * 0 will be returned on success, and a -ve er 224 * 0 will be returned on success, and a -ve error code if unsuccessful. 299 */ 225 */ 300 static int vfs_statx(int dfd, struct filename 226 static int vfs_statx(int dfd, struct filename *filename, int flags, 301 struct kstat *stat, u32 request_ 227 struct kstat *stat, u32 request_mask) 302 { 228 { 303 struct path path; 229 struct path path; 304 unsigned int lookup_flags = getname_st 230 unsigned int lookup_flags = getname_statx_lookup_flags(flags); 305 int error; 231 int error; 306 232 307 if (flags & ~(AT_SYMLINK_NOFOLLOW | AT 233 if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH | 308 AT_STATX_SYNC_TYPE)) 234 AT_STATX_SYNC_TYPE)) 309 return -EINVAL; 235 return -EINVAL; 310 236 311 retry: 237 retry: 312 error = filename_lookup(dfd, filename, 238 error = filename_lookup(dfd, filename, lookup_flags, &path, NULL); 313 if (error) 239 if (error) 314 return error; !! 240 goto out; 315 error = vfs_statx_path(&path, flags, s !! 241 >> 242 error = vfs_getattr(&path, stat, request_mask, flags); >> 243 >> 244 stat->mnt_id = real_mount(path.mnt)->mnt_id; >> 245 stat->result_mask |= STATX_MNT_ID; >> 246 >> 247 if (path.mnt->mnt_root == path.dentry) >> 248 stat->attributes |= STATX_ATTR_MOUNT_ROOT; >> 249 stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT; >> 250 >> 251 /* Handle STATX_DIOALIGN for block devices. */ >> 252 if (request_mask & STATX_DIOALIGN) { >> 253 struct inode *inode = d_backing_inode(path.dentry); >> 254 >> 255 if (S_ISBLK(inode->i_mode)) >> 256 bdev_statx_dioalign(inode, stat); >> 257 } >> 258 316 path_put(&path); 259 path_put(&path); 317 if (retry_estale(error, lookup_flags)) 260 if (retry_estale(error, lookup_flags)) { 318 lookup_flags |= LOOKUP_REVAL; 261 lookup_flags |= LOOKUP_REVAL; 319 goto retry; 262 goto retry; 320 } 263 } >> 264 out: 321 return error; 265 return error; 322 } 266 } 323 267 324 int vfs_fstatat(int dfd, const char __user *fi 268 int vfs_fstatat(int dfd, const char __user *filename, 325 struct kstat *st 269 struct kstat *stat, int flags) 326 { 270 { 327 int ret; 271 int ret; 328 int statx_flags = flags | AT_NO_AUTOMO 272 int statx_flags = flags | AT_NO_AUTOMOUNT; 329 struct filename *name; 273 struct filename *name; 330 274 331 /* !! 275 name = getname_flags(filename, getname_statx_lookup_flags(statx_flags), NULL); 332 * Work around glibc turning fstat() i << 333 * << 334 * If AT_EMPTY_PATH is set, we expect << 335 * empty path, and avoid doing all the << 336 */ << 337 if (flags == AT_EMPTY_PATH && vfs_empt << 338 return vfs_fstat(dfd, stat); << 339 << 340 name = getname_flags(filename, getname << 341 ret = vfs_statx(dfd, name, statx_flags 276 ret = vfs_statx(dfd, name, statx_flags, stat, STATX_BASIC_STATS); 342 putname(name); 277 putname(name); 343 278 344 return ret; 279 return ret; 345 } 280 } 346 281 347 #ifdef __ARCH_WANT_OLD_STAT 282 #ifdef __ARCH_WANT_OLD_STAT 348 283 349 /* 284 /* 350 * For backward compatibility? Maybe this sho 285 * For backward compatibility? Maybe this should be moved 351 * into arch/i386 instead? 286 * into arch/i386 instead? 352 */ 287 */ 353 static int cp_old_stat(struct kstat *stat, str 288 static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf) 354 { 289 { 355 static int warncount = 5; 290 static int warncount = 5; 356 struct __old_kernel_stat tmp; 291 struct __old_kernel_stat tmp; 357 292 358 if (warncount > 0) { 293 if (warncount > 0) { 359 warncount--; 294 warncount--; 360 printk(KERN_WARNING "VFS: Warn 295 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n", 361 current->comm); 296 current->comm); 362 } else if (warncount < 0) { 297 } else if (warncount < 0) { 363 /* it's laughable, but... */ 298 /* it's laughable, but... */ 364 warncount = 0; 299 warncount = 0; 365 } 300 } 366 301 367 memset(&tmp, 0, sizeof(struct __old_ke 302 memset(&tmp, 0, sizeof(struct __old_kernel_stat)); 368 tmp.st_dev = old_encode_dev(stat->dev) 303 tmp.st_dev = old_encode_dev(stat->dev); 369 tmp.st_ino = stat->ino; 304 tmp.st_ino = stat->ino; 370 if (sizeof(tmp.st_ino) < sizeof(stat-> 305 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) 371 return -EOVERFLOW; 306 return -EOVERFLOW; 372 tmp.st_mode = stat->mode; 307 tmp.st_mode = stat->mode; 373 tmp.st_nlink = stat->nlink; 308 tmp.st_nlink = stat->nlink; 374 if (tmp.st_nlink != stat->nlink) 309 if (tmp.st_nlink != stat->nlink) 375 return -EOVERFLOW; 310 return -EOVERFLOW; 376 SET_UID(tmp.st_uid, from_kuid_munged(c 311 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid)); 377 SET_GID(tmp.st_gid, from_kgid_munged(c 312 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid)); 378 tmp.st_rdev = old_encode_dev(stat->rde 313 tmp.st_rdev = old_encode_dev(stat->rdev); 379 #if BITS_PER_LONG == 32 314 #if BITS_PER_LONG == 32 380 if (stat->size > MAX_NON_LFS) 315 if (stat->size > MAX_NON_LFS) 381 return -EOVERFLOW; 316 return -EOVERFLOW; 382 #endif 317 #endif 383 tmp.st_size = stat->size; 318 tmp.st_size = stat->size; 384 tmp.st_atime = stat->atime.tv_sec; 319 tmp.st_atime = stat->atime.tv_sec; 385 tmp.st_mtime = stat->mtime.tv_sec; 320 tmp.st_mtime = stat->mtime.tv_sec; 386 tmp.st_ctime = stat->ctime.tv_sec; 321 tmp.st_ctime = stat->ctime.tv_sec; 387 return copy_to_user(statbuf,&tmp,sizeo 322 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; 388 } 323 } 389 324 390 SYSCALL_DEFINE2(stat, const char __user *, fil 325 SYSCALL_DEFINE2(stat, const char __user *, filename, 391 struct __old_kernel_stat __use 326 struct __old_kernel_stat __user *, statbuf) 392 { 327 { 393 struct kstat stat; 328 struct kstat stat; 394 int error; 329 int error; 395 330 396 error = vfs_stat(filename, &stat); 331 error = vfs_stat(filename, &stat); 397 if (error) 332 if (error) 398 return error; 333 return error; 399 334 400 return cp_old_stat(&stat, statbuf); 335 return cp_old_stat(&stat, statbuf); 401 } 336 } 402 337 403 SYSCALL_DEFINE2(lstat, const char __user *, fi 338 SYSCALL_DEFINE2(lstat, const char __user *, filename, 404 struct __old_kernel_stat __use 339 struct __old_kernel_stat __user *, statbuf) 405 { 340 { 406 struct kstat stat; 341 struct kstat stat; 407 int error; 342 int error; 408 343 409 error = vfs_lstat(filename, &stat); 344 error = vfs_lstat(filename, &stat); 410 if (error) 345 if (error) 411 return error; 346 return error; 412 347 413 return cp_old_stat(&stat, statbuf); 348 return cp_old_stat(&stat, statbuf); 414 } 349 } 415 350 416 SYSCALL_DEFINE2(fstat, unsigned int, fd, struc 351 SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf) 417 { 352 { 418 struct kstat stat; 353 struct kstat stat; 419 int error = vfs_fstat(fd, &stat); 354 int error = vfs_fstat(fd, &stat); 420 355 421 if (!error) 356 if (!error) 422 error = cp_old_stat(&stat, sta 357 error = cp_old_stat(&stat, statbuf); 423 358 424 return error; 359 return error; 425 } 360 } 426 361 427 #endif /* __ARCH_WANT_OLD_STAT */ 362 #endif /* __ARCH_WANT_OLD_STAT */ 428 363 429 #ifdef __ARCH_WANT_NEW_STAT 364 #ifdef __ARCH_WANT_NEW_STAT 430 365 >> 366 #if BITS_PER_LONG == 32 >> 367 # define choose_32_64(a,b) a >> 368 #else >> 369 # define choose_32_64(a,b) b >> 370 #endif >> 371 431 #ifndef INIT_STRUCT_STAT_PADDING 372 #ifndef INIT_STRUCT_STAT_PADDING 432 # define INIT_STRUCT_STAT_PADDING(st) memset( 373 # define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st)) 433 #endif 374 #endif 434 375 435 static int cp_new_stat(struct kstat *stat, str 376 static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf) 436 { 377 { 437 struct stat tmp; 378 struct stat tmp; 438 379 439 if (sizeof(tmp.st_dev) < 4 && !old_val 380 if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev)) 440 return -EOVERFLOW; 381 return -EOVERFLOW; 441 if (sizeof(tmp.st_rdev) < 4 && !old_va 382 if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev)) 442 return -EOVERFLOW; 383 return -EOVERFLOW; 443 #if BITS_PER_LONG == 32 384 #if BITS_PER_LONG == 32 444 if (stat->size > MAX_NON_LFS) 385 if (stat->size > MAX_NON_LFS) 445 return -EOVERFLOW; 386 return -EOVERFLOW; 446 #endif 387 #endif 447 388 448 INIT_STRUCT_STAT_PADDING(tmp); 389 INIT_STRUCT_STAT_PADDING(tmp); 449 tmp.st_dev = new_encode_dev(stat->dev) 390 tmp.st_dev = new_encode_dev(stat->dev); 450 tmp.st_ino = stat->ino; 391 tmp.st_ino = stat->ino; 451 if (sizeof(tmp.st_ino) < sizeof(stat-> 392 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) 452 return -EOVERFLOW; 393 return -EOVERFLOW; 453 tmp.st_mode = stat->mode; 394 tmp.st_mode = stat->mode; 454 tmp.st_nlink = stat->nlink; 395 tmp.st_nlink = stat->nlink; 455 if (tmp.st_nlink != stat->nlink) 396 if (tmp.st_nlink != stat->nlink) 456 return -EOVERFLOW; 397 return -EOVERFLOW; 457 SET_UID(tmp.st_uid, from_kuid_munged(c 398 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid)); 458 SET_GID(tmp.st_gid, from_kgid_munged(c 399 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid)); 459 tmp.st_rdev = new_encode_dev(stat->rde 400 tmp.st_rdev = new_encode_dev(stat->rdev); 460 tmp.st_size = stat->size; 401 tmp.st_size = stat->size; 461 tmp.st_atime = stat->atime.tv_sec; 402 tmp.st_atime = stat->atime.tv_sec; 462 tmp.st_mtime = stat->mtime.tv_sec; 403 tmp.st_mtime = stat->mtime.tv_sec; 463 tmp.st_ctime = stat->ctime.tv_sec; 404 tmp.st_ctime = stat->ctime.tv_sec; 464 #ifdef STAT_HAVE_NSEC 405 #ifdef STAT_HAVE_NSEC 465 tmp.st_atime_nsec = stat->atime.tv_nse 406 tmp.st_atime_nsec = stat->atime.tv_nsec; 466 tmp.st_mtime_nsec = stat->mtime.tv_nse 407 tmp.st_mtime_nsec = stat->mtime.tv_nsec; 467 tmp.st_ctime_nsec = stat->ctime.tv_nse 408 tmp.st_ctime_nsec = stat->ctime.tv_nsec; 468 #endif 409 #endif 469 tmp.st_blocks = stat->blocks; 410 tmp.st_blocks = stat->blocks; 470 tmp.st_blksize = stat->blksize; 411 tmp.st_blksize = stat->blksize; 471 return copy_to_user(statbuf,&tmp,sizeo 412 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; 472 } 413 } 473 414 474 SYSCALL_DEFINE2(newstat, const char __user *, 415 SYSCALL_DEFINE2(newstat, const char __user *, filename, 475 struct stat __user *, statbuf) 416 struct stat __user *, statbuf) 476 { 417 { 477 struct kstat stat; 418 struct kstat stat; 478 int error = vfs_stat(filename, &stat); 419 int error = vfs_stat(filename, &stat); 479 420 480 if (error) 421 if (error) 481 return error; 422 return error; 482 return cp_new_stat(&stat, statbuf); 423 return cp_new_stat(&stat, statbuf); 483 } 424 } 484 425 485 SYSCALL_DEFINE2(newlstat, const char __user *, 426 SYSCALL_DEFINE2(newlstat, const char __user *, filename, 486 struct stat __user *, statbuf) 427 struct stat __user *, statbuf) 487 { 428 { 488 struct kstat stat; 429 struct kstat stat; 489 int error; 430 int error; 490 431 491 error = vfs_lstat(filename, &stat); 432 error = vfs_lstat(filename, &stat); 492 if (error) 433 if (error) 493 return error; 434 return error; 494 435 495 return cp_new_stat(&stat, statbuf); 436 return cp_new_stat(&stat, statbuf); 496 } 437 } 497 438 498 #if !defined(__ARCH_WANT_STAT64) || defined(__ 439 #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT) 499 SYSCALL_DEFINE4(newfstatat, int, dfd, const ch 440 SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename, 500 struct stat __user *, statbuf, 441 struct stat __user *, statbuf, int, flag) 501 { 442 { 502 struct kstat stat; 443 struct kstat stat; 503 int error; 444 int error; 504 445 505 error = vfs_fstatat(dfd, filename, &st 446 error = vfs_fstatat(dfd, filename, &stat, flag); 506 if (error) 447 if (error) 507 return error; 448 return error; 508 return cp_new_stat(&stat, statbuf); 449 return cp_new_stat(&stat, statbuf); 509 } 450 } 510 #endif 451 #endif 511 452 512 SYSCALL_DEFINE2(newfstat, unsigned int, fd, st 453 SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf) 513 { 454 { 514 struct kstat stat; 455 struct kstat stat; 515 int error = vfs_fstat(fd, &stat); 456 int error = vfs_fstat(fd, &stat); 516 457 517 if (!error) 458 if (!error) 518 error = cp_new_stat(&stat, sta 459 error = cp_new_stat(&stat, statbuf); 519 460 520 return error; 461 return error; 521 } 462 } 522 #endif 463 #endif 523 464 524 static int do_readlinkat(int dfd, const char _ 465 static int do_readlinkat(int dfd, const char __user *pathname, 525 char __user *buf, int 466 char __user *buf, int bufsiz) 526 { 467 { 527 struct path path; 468 struct path path; 528 struct filename *name; << 529 int error; 469 int error; >> 470 int empty = 0; 530 unsigned int lookup_flags = LOOKUP_EMP 471 unsigned int lookup_flags = LOOKUP_EMPTY; 531 472 532 if (bufsiz <= 0) 473 if (bufsiz <= 0) 533 return -EINVAL; 474 return -EINVAL; 534 475 535 retry: 476 retry: 536 name = getname_flags(pathname, lookup_ !! 477 error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty); 537 error = filename_lookup(dfd, name, loo !! 478 if (!error) { 538 if (unlikely(error)) { !! 479 struct inode *inode = d_backing_inode(path.dentry); 539 putname(name); !! 480 540 return error; !! 481 error = empty ? -ENOENT : -EINVAL; 541 } !! 482 /* 542 !! 483 * AFS mountpoints allow readlink(2) but are not symlinks 543 /* !! 484 */ 544 * AFS mountpoints allow readlink(2) b !! 485 if (d_is_symlink(path.dentry) || inode->i_op->readlink) { 545 */ !! 486 error = security_inode_readlink(path.dentry); 546 if (d_is_symlink(path.dentry) || !! 487 if (!error) { 547 d_backing_inode(path.dentry)->i_op !! 488 touch_atime(&path); 548 error = security_inode_readlin !! 489 error = vfs_readlink(path.dentry, buf, bufsiz); 549 if (!error) { !! 490 } 550 touch_atime(&path); !! 491 } 551 error = vfs_readlink(p !! 492 path_put(&path); >> 493 if (retry_estale(error, lookup_flags)) { >> 494 lookup_flags |= LOOKUP_REVAL; >> 495 goto retry; 552 } 496 } 553 } else { << 554 error = (name->name[0] == '\0' << 555 } << 556 path_put(&path); << 557 putname(name); << 558 if (retry_estale(error, lookup_flags)) << 559 lookup_flags |= LOOKUP_REVAL; << 560 goto retry; << 561 } 497 } 562 return error; 498 return error; 563 } 499 } 564 500 565 SYSCALL_DEFINE4(readlinkat, int, dfd, const ch 501 SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname, 566 char __user *, buf, int, bufsi 502 char __user *, buf, int, bufsiz) 567 { 503 { 568 return do_readlinkat(dfd, pathname, bu 504 return do_readlinkat(dfd, pathname, buf, bufsiz); 569 } 505 } 570 506 571 SYSCALL_DEFINE3(readlink, const char __user *, 507 SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf, 572 int, bufsiz) 508 int, bufsiz) 573 { 509 { 574 return do_readlinkat(AT_FDCWD, path, b 510 return do_readlinkat(AT_FDCWD, path, buf, bufsiz); 575 } 511 } 576 512 577 513 578 /* ---------- LFS-64 ----------- */ 514 /* ---------- LFS-64 ----------- */ 579 #if defined(__ARCH_WANT_STAT64) || defined(__A 515 #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64) 580 516 581 #ifndef INIT_STRUCT_STAT64_PADDING 517 #ifndef INIT_STRUCT_STAT64_PADDING 582 # define INIT_STRUCT_STAT64_PADDING(st) memse 518 # define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st)) 583 #endif 519 #endif 584 520 585 static long cp_new_stat64(struct kstat *stat, 521 static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf) 586 { 522 { 587 struct stat64 tmp; 523 struct stat64 tmp; 588 524 589 INIT_STRUCT_STAT64_PADDING(tmp); 525 INIT_STRUCT_STAT64_PADDING(tmp); 590 #ifdef CONFIG_MIPS 526 #ifdef CONFIG_MIPS 591 /* mips has weird padding, so we don't 527 /* mips has weird padding, so we don't get 64 bits there */ 592 tmp.st_dev = new_encode_dev(stat->dev) 528 tmp.st_dev = new_encode_dev(stat->dev); 593 tmp.st_rdev = new_encode_dev(stat->rde 529 tmp.st_rdev = new_encode_dev(stat->rdev); 594 #else 530 #else 595 tmp.st_dev = huge_encode_dev(stat->dev 531 tmp.st_dev = huge_encode_dev(stat->dev); 596 tmp.st_rdev = huge_encode_dev(stat->rd 532 tmp.st_rdev = huge_encode_dev(stat->rdev); 597 #endif 533 #endif 598 tmp.st_ino = stat->ino; 534 tmp.st_ino = stat->ino; 599 if (sizeof(tmp.st_ino) < sizeof(stat-> 535 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) 600 return -EOVERFLOW; 536 return -EOVERFLOW; 601 #ifdef STAT64_HAS_BROKEN_ST_INO 537 #ifdef STAT64_HAS_BROKEN_ST_INO 602 tmp.__st_ino = stat->ino; 538 tmp.__st_ino = stat->ino; 603 #endif 539 #endif 604 tmp.st_mode = stat->mode; 540 tmp.st_mode = stat->mode; 605 tmp.st_nlink = stat->nlink; 541 tmp.st_nlink = stat->nlink; 606 tmp.st_uid = from_kuid_munged(current_ 542 tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid); 607 tmp.st_gid = from_kgid_munged(current_ 543 tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid); 608 tmp.st_atime = stat->atime.tv_sec; 544 tmp.st_atime = stat->atime.tv_sec; 609 tmp.st_atime_nsec = stat->atime.tv_nse 545 tmp.st_atime_nsec = stat->atime.tv_nsec; 610 tmp.st_mtime = stat->mtime.tv_sec; 546 tmp.st_mtime = stat->mtime.tv_sec; 611 tmp.st_mtime_nsec = stat->mtime.tv_nse 547 tmp.st_mtime_nsec = stat->mtime.tv_nsec; 612 tmp.st_ctime = stat->ctime.tv_sec; 548 tmp.st_ctime = stat->ctime.tv_sec; 613 tmp.st_ctime_nsec = stat->ctime.tv_nse 549 tmp.st_ctime_nsec = stat->ctime.tv_nsec; 614 tmp.st_size = stat->size; 550 tmp.st_size = stat->size; 615 tmp.st_blocks = stat->blocks; 551 tmp.st_blocks = stat->blocks; 616 tmp.st_blksize = stat->blksize; 552 tmp.st_blksize = stat->blksize; 617 return copy_to_user(statbuf,&tmp,sizeo 553 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; 618 } 554 } 619 555 620 SYSCALL_DEFINE2(stat64, const char __user *, f 556 SYSCALL_DEFINE2(stat64, const char __user *, filename, 621 struct stat64 __user *, statbu 557 struct stat64 __user *, statbuf) 622 { 558 { 623 struct kstat stat; 559 struct kstat stat; 624 int error = vfs_stat(filename, &stat); 560 int error = vfs_stat(filename, &stat); 625 561 626 if (!error) 562 if (!error) 627 error = cp_new_stat64(&stat, s 563 error = cp_new_stat64(&stat, statbuf); 628 564 629 return error; 565 return error; 630 } 566 } 631 567 632 SYSCALL_DEFINE2(lstat64, const char __user *, 568 SYSCALL_DEFINE2(lstat64, const char __user *, filename, 633 struct stat64 __user *, statbu 569 struct stat64 __user *, statbuf) 634 { 570 { 635 struct kstat stat; 571 struct kstat stat; 636 int error = vfs_lstat(filename, &stat) 572 int error = vfs_lstat(filename, &stat); 637 573 638 if (!error) 574 if (!error) 639 error = cp_new_stat64(&stat, s 575 error = cp_new_stat64(&stat, statbuf); 640 576 641 return error; 577 return error; 642 } 578 } 643 579 644 SYSCALL_DEFINE2(fstat64, unsigned long, fd, st 580 SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf) 645 { 581 { 646 struct kstat stat; 582 struct kstat stat; 647 int error = vfs_fstat(fd, &stat); 583 int error = vfs_fstat(fd, &stat); 648 584 649 if (!error) 585 if (!error) 650 error = cp_new_stat64(&stat, s 586 error = cp_new_stat64(&stat, statbuf); 651 587 652 return error; 588 return error; 653 } 589 } 654 590 655 SYSCALL_DEFINE4(fstatat64, int, dfd, const cha 591 SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename, 656 struct stat64 __user *, statbu 592 struct stat64 __user *, statbuf, int, flag) 657 { 593 { 658 struct kstat stat; 594 struct kstat stat; 659 int error; 595 int error; 660 596 661 error = vfs_fstatat(dfd, filename, &st 597 error = vfs_fstatat(dfd, filename, &stat, flag); 662 if (error) 598 if (error) 663 return error; 599 return error; 664 return cp_new_stat64(&stat, statbuf); 600 return cp_new_stat64(&stat, statbuf); 665 } 601 } 666 #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_CO 602 #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */ 667 603 668 static noinline_for_stack int 604 static noinline_for_stack int 669 cp_statx(const struct kstat *stat, struct stat 605 cp_statx(const struct kstat *stat, struct statx __user *buffer) 670 { 606 { 671 struct statx tmp; 607 struct statx tmp; 672 608 673 memset(&tmp, 0, sizeof(tmp)); 609 memset(&tmp, 0, sizeof(tmp)); 674 610 675 /* STATX_CHANGE_COOKIE is kernel-only 611 /* STATX_CHANGE_COOKIE is kernel-only for now */ 676 tmp.stx_mask = stat->result_mask & ~ST 612 tmp.stx_mask = stat->result_mask & ~STATX_CHANGE_COOKIE; 677 tmp.stx_blksize = stat->blksize; 613 tmp.stx_blksize = stat->blksize; 678 /* STATX_ATTR_CHANGE_MONOTONIC is kern 614 /* STATX_ATTR_CHANGE_MONOTONIC is kernel-only for now */ 679 tmp.stx_attributes = stat->attributes 615 tmp.stx_attributes = stat->attributes & ~STATX_ATTR_CHANGE_MONOTONIC; 680 tmp.stx_nlink = stat->nlink; 616 tmp.stx_nlink = stat->nlink; 681 tmp.stx_uid = from_kuid_munged(current 617 tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid); 682 tmp.stx_gid = from_kgid_munged(current 618 tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid); 683 tmp.stx_mode = stat->mode; 619 tmp.stx_mode = stat->mode; 684 tmp.stx_ino = stat->ino; 620 tmp.stx_ino = stat->ino; 685 tmp.stx_size = stat->size; 621 tmp.stx_size = stat->size; 686 tmp.stx_blocks = stat->blocks; 622 tmp.stx_blocks = stat->blocks; 687 tmp.stx_attributes_mask = stat->attrib 623 tmp.stx_attributes_mask = stat->attributes_mask; 688 tmp.stx_atime.tv_sec = stat->atime.tv_ 624 tmp.stx_atime.tv_sec = stat->atime.tv_sec; 689 tmp.stx_atime.tv_nsec = stat->atime.tv 625 tmp.stx_atime.tv_nsec = stat->atime.tv_nsec; 690 tmp.stx_btime.tv_sec = stat->btime.tv_ 626 tmp.stx_btime.tv_sec = stat->btime.tv_sec; 691 tmp.stx_btime.tv_nsec = stat->btime.tv 627 tmp.stx_btime.tv_nsec = stat->btime.tv_nsec; 692 tmp.stx_ctime.tv_sec = stat->ctime.tv_ 628 tmp.stx_ctime.tv_sec = stat->ctime.tv_sec; 693 tmp.stx_ctime.tv_nsec = stat->ctime.tv 629 tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec; 694 tmp.stx_mtime.tv_sec = stat->mtime.tv_ 630 tmp.stx_mtime.tv_sec = stat->mtime.tv_sec; 695 tmp.stx_mtime.tv_nsec = stat->mtime.tv 631 tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec; 696 tmp.stx_rdev_major = MAJOR(stat->rdev) 632 tmp.stx_rdev_major = MAJOR(stat->rdev); 697 tmp.stx_rdev_minor = MINOR(stat->rdev) 633 tmp.stx_rdev_minor = MINOR(stat->rdev); 698 tmp.stx_dev_major = MAJOR(stat->dev); 634 tmp.stx_dev_major = MAJOR(stat->dev); 699 tmp.stx_dev_minor = MINOR(stat->dev); 635 tmp.stx_dev_minor = MINOR(stat->dev); 700 tmp.stx_mnt_id = stat->mnt_id; 636 tmp.stx_mnt_id = stat->mnt_id; 701 tmp.stx_dio_mem_align = stat->dio_mem_ 637 tmp.stx_dio_mem_align = stat->dio_mem_align; 702 tmp.stx_dio_offset_align = stat->dio_o 638 tmp.stx_dio_offset_align = stat->dio_offset_align; 703 tmp.stx_subvol = stat->subvol; << 704 tmp.stx_atomic_write_unit_min = stat-> << 705 tmp.stx_atomic_write_unit_max = stat-> << 706 tmp.stx_atomic_write_segments_max = st << 707 639 708 return copy_to_user(buffer, &tmp, size 640 return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0; 709 } 641 } 710 642 711 int do_statx(int dfd, struct filename *filenam 643 int do_statx(int dfd, struct filename *filename, unsigned int flags, 712 unsigned int mask, struct statx _ 644 unsigned int mask, struct statx __user *buffer) 713 { 645 { 714 struct kstat stat; 646 struct kstat stat; 715 int error; 647 int error; 716 648 717 if (mask & STATX__RESERVED) 649 if (mask & STATX__RESERVED) 718 return -EINVAL; 650 return -EINVAL; 719 if ((flags & AT_STATX_SYNC_TYPE) == AT 651 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE) 720 return -EINVAL; 652 return -EINVAL; 721 653 722 /* !! 654 /* STATX_CHANGE_COOKIE is kernel-only for now. Ignore requests 723 * STATX_CHANGE_COOKIE is kernel-only << 724 * from userland. 655 * from userland. 725 */ 656 */ 726 mask &= ~STATX_CHANGE_COOKIE; 657 mask &= ~STATX_CHANGE_COOKIE; 727 658 728 error = vfs_statx(dfd, filename, flags 659 error = vfs_statx(dfd, filename, flags, &stat, mask); 729 if (error) 660 if (error) 730 return error; 661 return error; 731 662 732 return cp_statx(&stat, buffer); 663 return cp_statx(&stat, buffer); 733 } 664 } 734 665 735 int do_statx_fd(int fd, unsigned int flags, un << 736 struct statx __user *buffer) << 737 { << 738 struct kstat stat; << 739 int error; << 740 << 741 if (mask & STATX__RESERVED) << 742 return -EINVAL; << 743 if ((flags & AT_STATX_SYNC_TYPE) == AT << 744 return -EINVAL; << 745 << 746 /* << 747 * STATX_CHANGE_COOKIE is kernel-only << 748 * from userland. << 749 */ << 750 mask &= ~STATX_CHANGE_COOKIE; << 751 << 752 error = vfs_statx_fd(fd, flags, &stat, << 753 if (error) << 754 return error; << 755 << 756 return cp_statx(&stat, buffer); << 757 } << 758 << 759 /** 666 /** 760 * sys_statx - System call to get enhanced sta 667 * sys_statx - System call to get enhanced stats 761 * @dfd: Base directory to pathwalk from *or* 668 * @dfd: Base directory to pathwalk from *or* fd to stat. 762 * @filename: File to stat or either NULL or " !! 669 * @filename: File to stat or "" with AT_EMPTY_PATH 763 * @flags: AT_* flags to control pathwalk. 670 * @flags: AT_* flags to control pathwalk. 764 * @mask: Parts of statx struct actually requi 671 * @mask: Parts of statx struct actually required. 765 * @buffer: Result buffer. 672 * @buffer: Result buffer. 766 * 673 * 767 * Note that fstat() can be emulated by settin 674 * Note that fstat() can be emulated by setting dfd to the fd of interest, 768 * supplying "" (or preferably NULL) as the fi !! 675 * supplying "" as the filename and setting AT_EMPTY_PATH in the flags. 769 * in the flags. << 770 */ 676 */ 771 SYSCALL_DEFINE5(statx, 677 SYSCALL_DEFINE5(statx, 772 int, dfd, const char __user *, 678 int, dfd, const char __user *, filename, unsigned, flags, 773 unsigned int, mask, 679 unsigned int, mask, 774 struct statx __user *, buffer) 680 struct statx __user *, buffer) 775 { 681 { 776 int ret; 682 int ret; 777 unsigned lflags; << 778 struct filename *name; 683 struct filename *name; 779 684 780 /* !! 685 name = getname_flags(filename, getname_statx_lookup_flags(flags), NULL); 781 * Short-circuit handling of NULL and << 782 * << 783 * For a NULL path we require and acce << 784 * (possibly |'d with AT_STATX flags). << 785 * << 786 * However, glibc on 32-bit architectu << 787 * with the "" pathname and AT_NO_AUTO << 788 * Supporting this results in the ugli << 789 */ << 790 lflags = flags & ~(AT_NO_AUTOMOUNT | A << 791 if (lflags == AT_EMPTY_PATH && vfs_emp << 792 return do_statx_fd(dfd, flags << 793 << 794 name = getname_flags(filename, getname << 795 ret = do_statx(dfd, name, flags, mask, 686 ret = do_statx(dfd, name, flags, mask, buffer); 796 putname(name); 687 putname(name); 797 688 798 return ret; 689 return ret; 799 } 690 } 800 691 801 #if defined(CONFIG_COMPAT) && defined(__ARCH_W 692 #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_STAT) 802 static int cp_compat_stat(struct kstat *stat, 693 static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf) 803 { 694 { 804 struct compat_stat tmp; 695 struct compat_stat tmp; 805 696 806 if (sizeof(tmp.st_dev) < 4 && !old_val 697 if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev)) 807 return -EOVERFLOW; 698 return -EOVERFLOW; 808 if (sizeof(tmp.st_rdev) < 4 && !old_va 699 if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev)) 809 return -EOVERFLOW; 700 return -EOVERFLOW; 810 701 811 memset(&tmp, 0, sizeof(tmp)); 702 memset(&tmp, 0, sizeof(tmp)); 812 tmp.st_dev = new_encode_dev(stat->dev) 703 tmp.st_dev = new_encode_dev(stat->dev); 813 tmp.st_ino = stat->ino; 704 tmp.st_ino = stat->ino; 814 if (sizeof(tmp.st_ino) < sizeof(stat-> 705 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) 815 return -EOVERFLOW; 706 return -EOVERFLOW; 816 tmp.st_mode = stat->mode; 707 tmp.st_mode = stat->mode; 817 tmp.st_nlink = stat->nlink; 708 tmp.st_nlink = stat->nlink; 818 if (tmp.st_nlink != stat->nlink) 709 if (tmp.st_nlink != stat->nlink) 819 return -EOVERFLOW; 710 return -EOVERFLOW; 820 SET_UID(tmp.st_uid, from_kuid_munged(c 711 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid)); 821 SET_GID(tmp.st_gid, from_kgid_munged(c 712 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid)); 822 tmp.st_rdev = new_encode_dev(stat->rde 713 tmp.st_rdev = new_encode_dev(stat->rdev); 823 if ((u64) stat->size > MAX_NON_LFS) 714 if ((u64) stat->size > MAX_NON_LFS) 824 return -EOVERFLOW; 715 return -EOVERFLOW; 825 tmp.st_size = stat->size; 716 tmp.st_size = stat->size; 826 tmp.st_atime = stat->atime.tv_sec; 717 tmp.st_atime = stat->atime.tv_sec; 827 tmp.st_atime_nsec = stat->atime.tv_nse 718 tmp.st_atime_nsec = stat->atime.tv_nsec; 828 tmp.st_mtime = stat->mtime.tv_sec; 719 tmp.st_mtime = stat->mtime.tv_sec; 829 tmp.st_mtime_nsec = stat->mtime.tv_nse 720 tmp.st_mtime_nsec = stat->mtime.tv_nsec; 830 tmp.st_ctime = stat->ctime.tv_sec; 721 tmp.st_ctime = stat->ctime.tv_sec; 831 tmp.st_ctime_nsec = stat->ctime.tv_nse 722 tmp.st_ctime_nsec = stat->ctime.tv_nsec; 832 tmp.st_blocks = stat->blocks; 723 tmp.st_blocks = stat->blocks; 833 tmp.st_blksize = stat->blksize; 724 tmp.st_blksize = stat->blksize; 834 return copy_to_user(ubuf, &tmp, sizeof 725 return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0; 835 } 726 } 836 727 837 COMPAT_SYSCALL_DEFINE2(newstat, const char __u 728 COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename, 838 struct compat_stat __us 729 struct compat_stat __user *, statbuf) 839 { 730 { 840 struct kstat stat; 731 struct kstat stat; 841 int error; 732 int error; 842 733 843 error = vfs_stat(filename, &stat); 734 error = vfs_stat(filename, &stat); 844 if (error) 735 if (error) 845 return error; 736 return error; 846 return cp_compat_stat(&stat, statbuf); 737 return cp_compat_stat(&stat, statbuf); 847 } 738 } 848 739 849 COMPAT_SYSCALL_DEFINE2(newlstat, const char __ 740 COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename, 850 struct compat_stat __us 741 struct compat_stat __user *, statbuf) 851 { 742 { 852 struct kstat stat; 743 struct kstat stat; 853 int error; 744 int error; 854 745 855 error = vfs_lstat(filename, &stat); 746 error = vfs_lstat(filename, &stat); 856 if (error) 747 if (error) 857 return error; 748 return error; 858 return cp_compat_stat(&stat, statbuf); 749 return cp_compat_stat(&stat, statbuf); 859 } 750 } 860 751 861 #ifndef __ARCH_WANT_STAT64 752 #ifndef __ARCH_WANT_STAT64 862 COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned in 753 COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd, 863 const char __user *, fi 754 const char __user *, filename, 864 struct compat_stat __us 755 struct compat_stat __user *, statbuf, int, flag) 865 { 756 { 866 struct kstat stat; 757 struct kstat stat; 867 int error; 758 int error; 868 759 869 error = vfs_fstatat(dfd, filename, &st 760 error = vfs_fstatat(dfd, filename, &stat, flag); 870 if (error) 761 if (error) 871 return error; 762 return error; 872 return cp_compat_stat(&stat, statbuf); 763 return cp_compat_stat(&stat, statbuf); 873 } 764 } 874 #endif 765 #endif 875 766 876 COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, 767 COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd, 877 struct compat_stat __us 768 struct compat_stat __user *, statbuf) 878 { 769 { 879 struct kstat stat; 770 struct kstat stat; 880 int error = vfs_fstat(fd, &stat); 771 int error = vfs_fstat(fd, &stat); 881 772 882 if (!error) 773 if (!error) 883 error = cp_compat_stat(&stat, 774 error = cp_compat_stat(&stat, statbuf); 884 return error; 775 return error; 885 } 776 } 886 #endif 777 #endif 887 778 888 /* Caller is here responsible for sufficient l 779 /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */ 889 void __inode_add_bytes(struct inode *inode, lo 780 void __inode_add_bytes(struct inode *inode, loff_t bytes) 890 { 781 { 891 inode->i_blocks += bytes >> 9; 782 inode->i_blocks += bytes >> 9; 892 bytes &= 511; 783 bytes &= 511; 893 inode->i_bytes += bytes; 784 inode->i_bytes += bytes; 894 if (inode->i_bytes >= 512) { 785 if (inode->i_bytes >= 512) { 895 inode->i_blocks++; 786 inode->i_blocks++; 896 inode->i_bytes -= 512; 787 inode->i_bytes -= 512; 897 } 788 } 898 } 789 } 899 EXPORT_SYMBOL(__inode_add_bytes); 790 EXPORT_SYMBOL(__inode_add_bytes); 900 791 901 void inode_add_bytes(struct inode *inode, loff 792 void inode_add_bytes(struct inode *inode, loff_t bytes) 902 { 793 { 903 spin_lock(&inode->i_lock); 794 spin_lock(&inode->i_lock); 904 __inode_add_bytes(inode, bytes); 795 __inode_add_bytes(inode, bytes); 905 spin_unlock(&inode->i_lock); 796 spin_unlock(&inode->i_lock); 906 } 797 } 907 798 908 EXPORT_SYMBOL(inode_add_bytes); 799 EXPORT_SYMBOL(inode_add_bytes); 909 800 910 void __inode_sub_bytes(struct inode *inode, lo 801 void __inode_sub_bytes(struct inode *inode, loff_t bytes) 911 { 802 { 912 inode->i_blocks -= bytes >> 9; 803 inode->i_blocks -= bytes >> 9; 913 bytes &= 511; 804 bytes &= 511; 914 if (inode->i_bytes < bytes) { 805 if (inode->i_bytes < bytes) { 915 inode->i_blocks--; 806 inode->i_blocks--; 916 inode->i_bytes += 512; 807 inode->i_bytes += 512; 917 } 808 } 918 inode->i_bytes -= bytes; 809 inode->i_bytes -= bytes; 919 } 810 } 920 811 921 EXPORT_SYMBOL(__inode_sub_bytes); 812 EXPORT_SYMBOL(__inode_sub_bytes); 922 813 923 void inode_sub_bytes(struct inode *inode, loff 814 void inode_sub_bytes(struct inode *inode, loff_t bytes) 924 { 815 { 925 spin_lock(&inode->i_lock); 816 spin_lock(&inode->i_lock); 926 __inode_sub_bytes(inode, bytes); 817 __inode_sub_bytes(inode, bytes); 927 spin_unlock(&inode->i_lock); 818 spin_unlock(&inode->i_lock); 928 } 819 } 929 820 930 EXPORT_SYMBOL(inode_sub_bytes); 821 EXPORT_SYMBOL(inode_sub_bytes); 931 822 932 loff_t inode_get_bytes(struct inode *inode) 823 loff_t inode_get_bytes(struct inode *inode) 933 { 824 { 934 loff_t ret; 825 loff_t ret; 935 826 936 spin_lock(&inode->i_lock); 827 spin_lock(&inode->i_lock); 937 ret = __inode_get_bytes(inode); 828 ret = __inode_get_bytes(inode); 938 spin_unlock(&inode->i_lock); 829 spin_unlock(&inode->i_lock); 939 return ret; 830 return ret; 940 } 831 } 941 832 942 EXPORT_SYMBOL(inode_get_bytes); 833 EXPORT_SYMBOL(inode_get_bytes); 943 834 944 void inode_set_bytes(struct inode *inode, loff 835 void inode_set_bytes(struct inode *inode, loff_t bytes) 945 { 836 { 946 /* Caller is here responsible for suff 837 /* Caller is here responsible for sufficient locking 947 * (ie. inode->i_lock) */ 838 * (ie. inode->i_lock) */ 948 inode->i_blocks = bytes >> 9; 839 inode->i_blocks = bytes >> 9; 949 inode->i_bytes = bytes & 511; 840 inode->i_bytes = bytes & 511; 950 } 841 } 951 842 952 EXPORT_SYMBOL(inode_set_bytes); 843 EXPORT_SYMBOL(inode_set_bytes); 953 844
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.