1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2022-2024 Oracle.
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_shared.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_bmap_btree.h"
15 #include "xfs_inode.h"
16 #include "xfs_error.h"
17 #include "xfs_trace.h"
18 #include "xfs_trans.h"
19 #include "xfs_da_format.h"
20 #include "xfs_da_btree.h"
22 #include "xfs_ioctl.h"
23 #include "xfs_parent.h"
24 #include "xfs_da_btree.h"
25 #include "xfs_handle.h"
26 #include "xfs_health.h"
27 #include "xfs_icache.h"
28 #include "xfs_export.h"
29 #include "xfs_xattr.h"
32 #include <linux/namei.h>
35 xfs_filehandle_fid_len(void)
37 struct xfs_handle *handle = NULL;
39 return sizeof(struct xfs_fid) - sizeof(handle->ha_fid.fid_len);
47 struct xfs_handle *handle)
49 memcpy(&handle->ha_fsid, mp->m_fixedfsid, sizeof(struct xfs_fsid));
51 handle->ha_fid.fid_len = xfs_filehandle_fid_len();
52 handle->ha_fid.fid_pad = 0;
53 handle->ha_fid.fid_gen = gen;
54 handle->ha_fid.fid_ino = ino;
56 return sizeof(struct xfs_handle);
62 struct xfs_handle *handle)
64 memcpy(&handle->ha_fsid, mp->m_fixedfsid, sizeof(struct xfs_fsid));
65 memset(&handle->ha_fid, 0, sizeof(handle->ha_fid));
67 return sizeof(struct xfs_fsid);
71 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
72 * a file or fs handle.
74 * XFS_IOC_PATH_TO_FSHANDLE
75 * returns fs handle for a mount point or path within that mount point
76 * XFS_IOC_FD_TO_HANDLE
77 * returns full handle for a FD opened in user space
78 * XFS_IOC_PATH_TO_HANDLE
79 * returns full handle for a path
84 xfs_fsop_handlereq_t *hreq)
94 if (cmd == XFS_IOC_FD_TO_HANDLE) {
98 inode = file_inode(f.file);
100 error = user_path_at(AT_FDCWD, hreq->path, 0, &path);
103 inode = d_inode(path.dentry);
108 * We can only generate handles for inodes residing on a XFS filesystem,
109 * and only for regular files, directories or symbolic links.
112 if (inode->i_sb->s_magic != XFS_SB_MAGIC)
116 if (!S_ISREG(inode->i_mode) &&
117 !S_ISDIR(inode->i_mode) &&
118 !S_ISLNK(inode->i_mode))
122 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
124 if (cmd == XFS_IOC_PATH_TO_FSHANDLE)
125 hsize = xfs_fshandle_init(ip->i_mount, &handle);
127 hsize = xfs_filehandle_init(ip->i_mount, ip->i_ino,
128 inode->i_generation, &handle);
131 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
132 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
138 if (cmd == XFS_IOC_FD_TO_HANDLE)
146 * No need to do permission checks on the various pathname components
147 * as the handle operations are privileged.
150 xfs_handle_acceptable(
152 struct dentry *dentry)
157 /* Convert handle already copied to kernel space into a dentry. */
158 static struct dentry *
159 xfs_khandle_to_dentry(
161 struct xfs_handle *handle)
163 struct xfs_fid64 fid = {
164 .ino = handle->ha_fid.fid_ino,
165 .gen = handle->ha_fid.fid_gen,
169 * Only allow handle opens under a directory.
171 if (!S_ISDIR(file_inode(file)->i_mode))
172 return ERR_PTR(-ENOTDIR);
174 if (handle->ha_fid.fid_len != xfs_filehandle_fid_len())
175 return ERR_PTR(-EINVAL);
177 return exportfs_decode_fh(file->f_path.mnt, (struct fid *)&fid, 3,
178 FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
179 xfs_handle_acceptable, NULL);
182 /* Convert handle already copied to kernel space into an xfs_inode. */
183 static struct xfs_inode *
184 xfs_khandle_to_inode(
186 struct xfs_handle *handle)
188 struct xfs_inode *ip = XFS_I(file_inode(file));
189 struct xfs_mount *mp = ip->i_mount;
192 if (!S_ISDIR(VFS_I(ip)->i_mode))
193 return ERR_PTR(-ENOTDIR);
195 if (handle->ha_fid.fid_len != xfs_filehandle_fid_len())
196 return ERR_PTR(-EINVAL);
198 inode = xfs_nfs_get_inode(mp->m_super, handle->ha_fid.fid_ino,
199 handle->ha_fid.fid_gen);
201 return ERR_CAST(inode);
207 * Convert userspace handle data into a dentry.
210 xfs_handle_to_dentry(
211 struct file *parfilp,
212 void __user *uhandle,
217 if (hlen != sizeof(xfs_handle_t))
218 return ERR_PTR(-EINVAL);
219 if (copy_from_user(&handle, uhandle, hlen))
220 return ERR_PTR(-EFAULT);
222 return xfs_khandle_to_dentry(parfilp, &handle);
225 STATIC struct dentry *
226 xfs_handlereq_to_dentry(
227 struct file *parfilp,
228 xfs_fsop_handlereq_t *hreq)
230 return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
235 struct file *parfilp,
236 xfs_fsop_handlereq_t *hreq)
238 const struct cred *cred = current_cred();
244 struct dentry *dentry;
248 if (!capable(CAP_SYS_ADMIN))
251 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
253 return PTR_ERR(dentry);
254 inode = d_inode(dentry);
256 /* Restrict xfs_open_by_handle to directories & regular files. */
257 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
262 #if BITS_PER_LONG != 32
263 hreq->oflags |= O_LARGEFILE;
266 permflag = hreq->oflags;
267 fmode = OPEN_FMODE(permflag);
268 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
269 (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
274 if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
279 /* Can't write directories. */
280 if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
285 fd = get_unused_fd_flags(0);
291 path.mnt = parfilp->f_path.mnt;
292 path.dentry = dentry;
293 filp = dentry_open(&path, hreq->oflags, cred);
297 return PTR_ERR(filp);
300 if (S_ISREG(inode->i_mode)) {
301 filp->f_flags |= O_NOATIME;
302 filp->f_mode |= FMODE_NOCMTIME;
305 fd_install(fd, filp);
314 xfs_readlink_by_handle(
315 struct file *parfilp,
316 xfs_fsop_handlereq_t *hreq)
318 struct dentry *dentry;
322 if (!capable(CAP_SYS_ADMIN))
325 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
327 return PTR_ERR(dentry);
329 /* Restrict this handle operation to symlinks only. */
330 if (!d_is_symlink(dentry)) {
335 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
340 error = vfs_readlink(dentry, hreq->ohandle, olen);
348 * Format an attribute and copy it out to the user's buffer.
349 * Take care to check values and protect against them changing later,
350 * we may be reading them directly out of a user buffer.
353 xfs_ioc_attr_put_listent(
354 struct xfs_attr_list_context *context,
361 struct xfs_attrlist *alist = context->buffer;
362 struct xfs_attrlist_ent *aep;
365 ASSERT(!context->seen_enough);
366 ASSERT(context->count >= 0);
367 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
368 ASSERT(context->firstu >= sizeof(*alist));
369 ASSERT(context->firstu <= context->bufsize);
372 * Only list entries in the right namespace.
374 if (context->attr_filter != (flags & XFS_ATTR_NSP_ONDISK_MASK))
377 arraytop = sizeof(*alist) +
378 context->count * sizeof(alist->al_offset[0]);
380 /* decrement by the actual bytes used by the attr */
381 context->firstu -= round_up(offsetof(struct xfs_attrlist_ent, a_name) +
382 namelen + 1, sizeof(uint32_t));
383 if (context->firstu < arraytop) {
384 trace_xfs_attr_list_full(context);
386 context->seen_enough = 1;
390 aep = context->buffer + context->firstu;
391 aep->a_valuelen = valuelen;
392 memcpy(aep->a_name, name, namelen);
393 aep->a_name[namelen] = 0;
394 alist->al_offset[context->count++] = context->firstu;
395 alist->al_count = context->count;
396 trace_xfs_attr_list_add(context);
403 if (ioc_flags & XFS_IOC_ATTR_ROOT)
404 return XFS_ATTR_ROOT;
405 if (ioc_flags & XFS_IOC_ATTR_SECURE)
406 return XFS_ATTR_SECURE;
410 static inline enum xfs_attr_update
416 return XFS_ATTRUPDATE_REMOVE;
417 if (ioc_flags & XFS_IOC_ATTR_CREATE)
418 return XFS_ATTRUPDATE_CREATE;
419 if (ioc_flags & XFS_IOC_ATTR_REPLACE)
420 return XFS_ATTRUPDATE_REPLACE;
421 return XFS_ATTRUPDATE_UPSERT;
426 struct xfs_inode *dp,
430 struct xfs_attrlist_cursor __user *ucursor)
432 struct xfs_attr_list_context context = { };
433 struct xfs_attrlist *alist;
437 if (bufsize < sizeof(struct xfs_attrlist) ||
438 bufsize > XFS_XATTR_LIST_MAX)
442 * Reject flags, only allow namespaces.
444 if (flags & ~(XFS_IOC_ATTR_ROOT | XFS_IOC_ATTR_SECURE))
446 if (flags == (XFS_IOC_ATTR_ROOT | XFS_IOC_ATTR_SECURE))
450 * Validate the cursor.
452 if (copy_from_user(&context.cursor, ucursor, sizeof(context.cursor)))
454 if (context.cursor.pad1 || context.cursor.pad2)
456 if (!context.cursor.initted &&
457 (context.cursor.hashval || context.cursor.blkno ||
458 context.cursor.offset))
461 buffer = kvzalloc(bufsize, GFP_KERNEL);
466 * Initialize the output buffer.
470 context.attr_filter = xfs_attr_filter(flags);
471 context.buffer = buffer;
472 context.bufsize = round_down(bufsize, sizeof(uint32_t));
473 context.firstu = context.bufsize;
474 context.put_listent = xfs_ioc_attr_put_listent;
476 alist = context.buffer;
479 alist->al_offset[0] = context.bufsize;
481 error = xfs_attr_list(&context);
485 if (copy_to_user(ubuf, buffer, bufsize) ||
486 copy_to_user(ucursor, &context.cursor, sizeof(context.cursor)))
494 xfs_attrlist_by_handle(
495 struct file *parfilp,
496 struct xfs_fsop_attrlist_handlereq __user *p)
498 struct xfs_fsop_attrlist_handlereq al_hreq;
499 struct dentry *dentry;
502 if (!capable(CAP_SYS_ADMIN))
504 if (copy_from_user(&al_hreq, p, sizeof(al_hreq)))
507 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
509 return PTR_ERR(dentry);
511 error = xfs_ioc_attr_list(XFS_I(d_inode(dentry)), al_hreq.buffer,
512 al_hreq.buflen, al_hreq.flags, &p->pos);
518 xfs_attrmulti_attr_get(
521 unsigned char __user *ubuf,
525 struct xfs_da_args args = {
527 .attr_filter = xfs_attr_filter(flags),
529 .namelen = strlen(name),
534 if (*len > XFS_XATTR_SIZE_MAX)
537 error = xfs_attr_get(&args);
541 *len = args.valuelen;
542 if (copy_to_user(ubuf, args.value, args.valuelen))
551 xfs_attrmulti_attr_set(
554 const unsigned char __user *ubuf,
558 struct xfs_da_args args = {
560 .attr_filter = xfs_attr_filter(flags),
562 .namelen = strlen(name),
566 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
570 if (len > XFS_XATTR_SIZE_MAX)
572 args.value = memdup_user(ubuf, len);
573 if (IS_ERR(args.value))
574 return PTR_ERR(args.value);
578 error = xfs_attr_change(&args, xfs_xattr_flags(flags, args.value));
579 if (!error && (flags & XFS_IOC_ATTR_ROOT))
580 xfs_forget_acl(inode, name);
586 xfs_ioc_attrmulti_one(
587 struct file *parfilp,
598 if ((flags & XFS_IOC_ATTR_ROOT) && (flags & XFS_IOC_ATTR_SECURE))
601 name = strndup_user(uname, MAXNAMELEN);
603 return PTR_ERR(name);
607 error = xfs_attrmulti_attr_get(inode, name, value, len, flags);
614 error = mnt_want_write_file(parfilp);
617 error = xfs_attrmulti_attr_set(inode, name, value, *len, flags);
618 mnt_drop_write_file(parfilp);
630 xfs_attrmulti_by_handle(
631 struct file *parfilp,
635 xfs_attr_multiop_t *ops;
636 xfs_fsop_attrmulti_handlereq_t am_hreq;
637 struct dentry *dentry;
638 unsigned int i, size;
640 if (!capable(CAP_SYS_ADMIN))
642 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
646 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
649 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
651 return PTR_ERR(dentry);
654 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
655 if (!size || size > 16 * PAGE_SIZE)
658 ops = memdup_user(am_hreq.ops, size);
660 error = PTR_ERR(ops);
665 for (i = 0; i < am_hreq.opcount; i++) {
666 ops[i].am_error = xfs_ioc_attrmulti_one(parfilp,
667 d_inode(dentry), ops[i].am_opcode,
668 ops[i].am_attrname, ops[i].am_attrvalue,
669 &ops[i].am_length, ops[i].am_flags);
672 if (copy_to_user(am_hreq.ops, ops, size))
681 struct xfs_getparents_ctx {
682 struct xfs_attr_list_context context;
683 struct xfs_getparents_by_handle gph;
686 struct xfs_inode *ip;
688 /* Internal buffer where we format records */
691 /* Last record filled out */
692 struct xfs_getparents_rec *lastrec;
697 static inline unsigned int
698 xfs_getparents_rec_sizeof(
699 unsigned int namelen)
701 return round_up(sizeof(struct xfs_getparents_rec) + namelen + 1,
706 xfs_getparents_put_listent(
707 struct xfs_attr_list_context *context,
714 struct xfs_getparents_ctx *gpx =
715 container_of(context, struct xfs_getparents_ctx, context);
716 struct xfs_inode *ip = context->dp;
717 struct xfs_mount *mp = ip->i_mount;
718 struct xfs_getparents *gp = &gpx->gph.gph_request;
719 struct xfs_getparents_rec *gpr = gpx->krecords + context->firstu;
720 unsigned short reclen =
721 xfs_getparents_rec_sizeof(namelen);
726 if (!(flags & XFS_ATTR_PARENT))
729 error = xfs_parent_from_attr(mp, flags, name, namelen, value, valuelen,
732 xfs_inode_mark_sick(ip, XFS_SICK_INO_PARENT);
733 context->seen_enough = -EFSCORRUPTED;
738 * We found a parent pointer, but we've filled up the buffer. Signal
739 * to the caller that we did /not/ reach the end of the parent pointer
742 if (context->firstu > context->bufsize - reclen) {
743 context->seen_enough = 1;
747 /* Format the parent pointer directly into the caller buffer. */
748 gpr->gpr_reclen = reclen;
749 xfs_filehandle_init(mp, ino, gen, &gpr->gpr_parent);
750 memcpy(gpr->gpr_name, name, namelen);
751 gpr->gpr_name[namelen] = 0;
753 trace_xfs_getparents_put_listent(ip, gp, context, gpr);
755 context->firstu += reclen;
760 /* Expand the last record to fill the rest of the caller's buffer. */
762 xfs_getparents_expand_lastrec(
763 struct xfs_getparents_ctx *gpx)
765 struct xfs_getparents *gp = &gpx->gph.gph_request;
766 struct xfs_getparents_rec *gpr = gpx->lastrec;
771 gpr->gpr_reclen = gp->gp_bufsize - ((void *)gpr - gpx->krecords);
773 trace_xfs_getparents_expand_lastrec(gpx->ip, gp, &gpx->context, gpr);
776 static inline void __user *u64_to_uptr(u64 val)
778 return (void __user *)(uintptr_t)val;
781 /* Retrieve the parent pointers for a given inode. */
784 struct xfs_getparents_ctx *gpx)
786 struct xfs_getparents *gp = &gpx->gph.gph_request;
787 struct xfs_inode *ip = gpx->ip;
788 struct xfs_mount *mp = ip->i_mount;
792 /* Check size of buffer requested by user */
793 if (gp->gp_bufsize > XFS_XATTR_LIST_MAX)
795 if (gp->gp_bufsize < xfs_getparents_rec_sizeof(1))
798 if (gp->gp_iflags & ~XFS_GETPARENTS_IFLAGS_ALL)
803 bufsize = round_down(gp->gp_bufsize, sizeof(uint64_t));
804 gpx->krecords = kvzalloc(bufsize, GFP_KERNEL);
805 if (!gpx->krecords) {
806 bufsize = min(bufsize, PAGE_SIZE);
807 gpx->krecords = kvzalloc(bufsize, GFP_KERNEL);
812 gpx->context.dp = ip;
813 gpx->context.resynch = 1;
814 gpx->context.put_listent = xfs_getparents_put_listent;
815 gpx->context.bufsize = bufsize;
816 /* firstu is used to track the bytes filled in the buffer */
817 gpx->context.firstu = 0;
819 /* Copy the cursor provided by caller */
820 memcpy(&gpx->context.cursor, &gp->gp_cursor,
821 sizeof(struct xfs_attrlist_cursor));
825 trace_xfs_getparents_begin(ip, gp, &gpx->context.cursor);
827 error = xfs_attr_list(&gpx->context);
830 if (gpx->context.seen_enough < 0) {
831 error = gpx->context.seen_enough;
834 xfs_getparents_expand_lastrec(gpx);
836 /* Update the caller with the current cursor position */
837 memcpy(&gp->gp_cursor, &gpx->context.cursor,
838 sizeof(struct xfs_attrlist_cursor));
840 /* Is this the root directory? */
841 if (ip->i_ino == mp->m_sb.sb_rootino)
842 gp->gp_oflags |= XFS_GETPARENTS_OFLAG_ROOT;
844 if (gpx->context.seen_enough == 0) {
846 * If we did not run out of buffer space, then we reached the
847 * end of the pptr recordset, so set the DONE flag.
849 gp->gp_oflags |= XFS_GETPARENTS_OFLAG_DONE;
850 } else if (gpx->count == 0) {
852 * If we ran out of buffer space before copying any parent
853 * pointers at all, the caller's buffer was too short. Tell
854 * userspace that, erm, the message is too long.
860 trace_xfs_getparents_end(ip, gp, &gpx->context.cursor);
862 ASSERT(gpx->context.firstu <= gpx->gph.gph_request.gp_bufsize);
864 /* Copy the records to userspace. */
865 if (copy_to_user(u64_to_uptr(gpx->gph.gph_request.gp_buffer),
866 gpx->krecords, gpx->context.firstu))
870 kvfree(gpx->krecords);
871 gpx->krecords = NULL;
875 /* Retrieve the parents of this file and pass them back to userspace. */
879 struct xfs_getparents __user *ureq)
881 struct xfs_getparents_ctx gpx = {
882 .ip = XFS_I(file_inode(file)),
884 struct xfs_getparents *kreq = &gpx.gph.gph_request;
885 struct xfs_mount *mp = gpx.ip->i_mount;
888 if (!capable(CAP_SYS_ADMIN))
890 if (!xfs_has_parent(mp))
892 if (copy_from_user(kreq, ureq, sizeof(*kreq)))
895 error = xfs_getparents(&gpx);
899 if (copy_to_user(ureq, kreq, sizeof(*kreq)))
905 /* Retrieve the parents of this file handle and pass them back to userspace. */
907 xfs_ioc_getparents_by_handle(
909 struct xfs_getparents_by_handle __user *ureq)
911 struct xfs_getparents_ctx gpx = { };
912 struct xfs_inode *ip = XFS_I(file_inode(file));
913 struct xfs_mount *mp = ip->i_mount;
914 struct xfs_getparents_by_handle *kreq = &gpx.gph;
915 struct xfs_handle *handle = &kreq->gph_handle;
918 if (!capable(CAP_SYS_ADMIN))
920 if (!xfs_has_parent(mp))
922 if (copy_from_user(kreq, ureq, sizeof(*kreq)))
926 * We don't use exportfs_decode_fh because it does too much work here.
927 * If the handle refers to a directory, the exportfs code will walk
928 * upwards through the directory tree to connect the dentries to the
929 * root directory dentry. For GETPARENTS we don't care about that
930 * because we're not actually going to open a file descriptor; we only
931 * want to open an inode and read its parent pointers.
933 * Note that xfs_scrub uses GETPARENTS to log that it will try to fix a
934 * corrupted file's metadata. For this usecase we would really rather
935 * userspace single-step the path reconstruction to avoid loops or
936 * other strange things if the directory tree is corrupt.
938 gpx.ip = xfs_khandle_to_inode(file, handle);
940 return PTR_ERR(gpx.ip);
942 error = xfs_getparents(&gpx);
946 if (copy_to_user(ureq, kreq, sizeof(*kreq)))