1/*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_bit.h"
21#include "xfs_log.h"
22#include "xfs_inum.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
26#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_quota.h"
30#include "xfs_mount.h"
31#include "xfs_bmap_btree.h"
32#include "xfs_alloc_btree.h"
33#include "xfs_ialloc_btree.h"
34#include "xfs_dir2_sf.h"
35#include "xfs_attr_sf.h"
36#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_bmap.h"
39#include "xfs_btree.h"
40#include "xfs_ialloc.h"
41#include "xfs_rtalloc.h"
42#include "xfs_error.h"
43#include "xfs_itable.h"
44#include "xfs_rw.h"
45#include "xfs_acl.h"
46#include "xfs_attr.h"
47#include "xfs_buf_item.h"
48#include "xfs_utils.h"
49
50#include <linux/capability.h>
51#include <linux/xattr.h>
52#include <linux/namei.h>
53#include <linux/security.h>
54
55/*
56 * Get a XFS inode from a given vnode.
57 */
58xfs_inode_t *
59xfs_vtoi(
60	bhv_vnode_t	*vp)
61{
62	bhv_desc_t      *bdp;
63
64	bdp = bhv_lookup_range(VN_BHV_HEAD(vp),
65			VNODE_POSITION_XFS, VNODE_POSITION_XFS);
66	if (unlikely(bdp == NULL))
67		return NULL;
68	return XFS_BHVTOI(bdp);
69}
70
71/*
72 * Bring the atime in the XFS inode uptodate.
73 * Used before logging the inode to disk or when the Linux inode goes away.
74 */
75void
76xfs_synchronize_atime(
77	xfs_inode_t	*ip)
78{
79	bhv_vnode_t	*vp;
80
81	vp = XFS_ITOV_NULL(ip);
82	if (vp) {
83		struct inode *inode = &vp->v_inode;
84		ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
85		ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
86	}
87}
88
89/*
90 * Change the requested timestamp in the given inode.
91 * We don't lock across timestamp updates, and we don't log them but
92 * we do record the fact that there is dirty information in core.
93 *
94 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
95 *		with XFS_ICHGTIME_ACC to be sure that access time
96 *		update will take.  Calling first with XFS_ICHGTIME_ACC
97 *		and then XFS_ICHGTIME_MOD may fail to modify the access
98 *		timestamp if the filesystem is mounted noacctm.
99 */
100void
101xfs_ichgtime(
102	xfs_inode_t	*ip,
103	int		flags)
104{
105	struct inode	*inode = vn_to_inode(XFS_ITOV(ip));
106	timespec_t	tv;
107
108	nanotime(&tv);
109	if (flags & XFS_ICHGTIME_MOD) {
110		inode->i_mtime = tv;
111		ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
112		ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
113	}
114	if (flags & XFS_ICHGTIME_ACC) {
115		inode->i_atime = tv;
116		ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
117		ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
118	}
119	if (flags & XFS_ICHGTIME_CHG) {
120		inode->i_ctime = tv;
121		ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
122		ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
123	}
124
125	/*
126	 * We update the i_update_core field _after_ changing
127	 * the timestamps in order to coordinate properly with
128	 * xfs_iflush() so that we don't lose timestamp updates.
129	 * This keeps us from having to hold the inode lock
130	 * while doing this.  We use the SYNCHRONIZE macro to
131	 * ensure that the compiler does not reorder the update
132	 * of i_update_core above the timestamp updates above.
133	 */
134	SYNCHRONIZE();
135	ip->i_update_core = 1;
136	if (!(inode->i_state & I_LOCK))
137		mark_inode_dirty_sync(inode);
138}
139
140/*
141 * Variant on the above which avoids querying the system clock
142 * in situations where we know the Linux inode timestamps have
143 * just been updated (and so we can update our inode cheaply).
144 */
145void
146xfs_ichgtime_fast(
147	xfs_inode_t	*ip,
148	struct inode	*inode,
149	int		flags)
150{
151	timespec_t	*tvp;
152
153	/*
154	 * Atime updates for read() & friends are handled lazily now, and
155	 * explicit updates must go through xfs_ichgtime()
156	 */
157	ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
158
159	/*
160	 * We're not supposed to change timestamps in readonly-mounted
161	 * filesystems.  Throw it away if anyone asks us.
162	 */
163	if (unlikely(IS_RDONLY(inode)))
164		return;
165
166	if (flags & XFS_ICHGTIME_MOD) {
167		tvp = &inode->i_mtime;
168		ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
169		ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
170	}
171	if (flags & XFS_ICHGTIME_CHG) {
172		tvp = &inode->i_ctime;
173		ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
174		ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
175	}
176
177	/*
178	 * We update the i_update_core field _after_ changing
179	 * the timestamps in order to coordinate properly with
180	 * xfs_iflush() so that we don't lose timestamp updates.
181	 * This keeps us from having to hold the inode lock
182	 * while doing this.  We use the SYNCHRONIZE macro to
183	 * ensure that the compiler does not reorder the update
184	 * of i_update_core above the timestamp updates above.
185	 */
186	SYNCHRONIZE();
187	ip->i_update_core = 1;
188	if (!(inode->i_state & I_LOCK))
189		mark_inode_dirty_sync(inode);
190}
191
192
193/*
194 * Pull the link count and size up from the xfs inode to the linux inode
195 */
196STATIC void
197xfs_validate_fields(
198	struct inode	*ip,
199	bhv_vattr_t	*vattr)
200{
201	vattr->va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
202	if (!bhv_vop_getattr(vn_from_inode(ip), vattr, ATTR_LAZY, NULL)) {
203		ip->i_nlink = vattr->va_nlink;
204		ip->i_blocks = vattr->va_nblocks;
205
206		/* we're under i_sem so i_size can't change under us */
207		if (i_size_read(ip) != vattr->va_size)
208			i_size_write(ip, vattr->va_size);
209	}
210}
211
212/*
213 * Hook in SELinux.  This is not quite correct yet, what we really need
214 * here (as we do for default ACLs) is a mechanism by which creation of
215 * these attrs can be journalled at inode creation time (along with the
216 * inode, of course, such that log replay can't cause these to be lost).
217 */
218STATIC int
219xfs_init_security(
220	bhv_vnode_t	*vp,
221	struct inode	*dir)
222{
223	struct inode	*ip = vn_to_inode(vp);
224	size_t		length;
225	void		*value;
226	char		*name;
227	int		error;
228
229	error = security_inode_init_security(ip, dir, &name, &value, &length);
230	if (error) {
231		if (error == -EOPNOTSUPP)
232			return 0;
233		return -error;
234	}
235
236	error = bhv_vop_attr_set(vp, name, value, length, ATTR_SECURE, NULL);
237	if (!error)
238		VMODIFY(vp);
239
240	kfree(name);
241	kfree(value);
242	return error;
243}
244
245STATIC_INLINE int
246xfs_has_fs_struct(struct task_struct *task)
247{
248	return (task->fs != init_task.fs);
249}
250
251STATIC void
252xfs_cleanup_inode(
253	bhv_vnode_t	*dvp,
254	bhv_vnode_t	*vp,
255	struct dentry	*dentry,
256	int		mode)
257{
258	struct dentry   teardown = {};
259
260	/* Oh, the horror.
261	 * If we can't add the ACL or we fail in
262	 * xfs_init_security we must back out.
263	 * ENOSPC can hit here, among other things.
264	 */
265	teardown.d_inode = vn_to_inode(vp);
266	teardown.d_name = dentry->d_name;
267
268	if (S_ISDIR(mode))
269	  	bhv_vop_rmdir(dvp, &teardown, NULL);
270	else
271		bhv_vop_remove(dvp, &teardown, NULL);
272	VN_RELE(vp);
273}
274
275STATIC int
276xfs_vn_mknod(
277	struct inode	*dir,
278	struct dentry	*dentry,
279	int		mode,
280	dev_t		rdev)
281{
282	struct inode	*ip;
283	bhv_vattr_t	vattr = { 0 };
284	bhv_vnode_t	*vp = NULL, *dvp = vn_from_inode(dir);
285	xfs_acl_t	*default_acl = NULL;
286	attrexists_t	test_default_acl = _ACL_DEFAULT_EXISTS;
287	int		error;
288
289	/*
290	 * Irix uses Missed'em'V split, but doesn't want to see
291	 * the upper 5 bits of (14bit) major.
292	 */
293	if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
294		return -EINVAL;
295
296	if (unlikely(test_default_acl && test_default_acl(dvp))) {
297		if (!_ACL_ALLOC(default_acl)) {
298			return -ENOMEM;
299		}
300		if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
301			_ACL_FREE(default_acl);
302			default_acl = NULL;
303		}
304	}
305
306	if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
307		mode &= ~current->fs->umask;
308
309	vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
310	vattr.va_mode = mode;
311
312	switch (mode & S_IFMT) {
313	case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
314		vattr.va_rdev = sysv_encode_dev(rdev);
315		vattr.va_mask |= XFS_AT_RDEV;
316		/*FALLTHROUGH*/
317	case S_IFREG:
318		error = bhv_vop_create(dvp, dentry, &vattr, &vp, NULL);
319		break;
320	case S_IFDIR:
321		error = bhv_vop_mkdir(dvp, dentry, &vattr, &vp, NULL);
322		break;
323	default:
324		error = EINVAL;
325		break;
326	}
327
328	if (unlikely(!error)) {
329		error = xfs_init_security(vp, dir);
330		if (error)
331			xfs_cleanup_inode(dvp, vp, dentry, mode);
332	}
333
334	if (unlikely(default_acl)) {
335		if (!error) {
336			error = _ACL_INHERIT(vp, &vattr, default_acl);
337			if (!error)
338				VMODIFY(vp);
339			else
340				xfs_cleanup_inode(dvp, vp, dentry, mode);
341		}
342		_ACL_FREE(default_acl);
343	}
344
345	if (likely(!error)) {
346		ASSERT(vp);
347		ip = vn_to_inode(vp);
348
349		if (S_ISCHR(mode) || S_ISBLK(mode))
350			ip->i_rdev = rdev;
351		else if (S_ISDIR(mode))
352			xfs_validate_fields(ip, &vattr);
353		d_instantiate(dentry, ip);
354		xfs_validate_fields(dir, &vattr);
355	}
356	return -error;
357}
358
359STATIC int
360xfs_vn_create(
361	struct inode	*dir,
362	struct dentry	*dentry,
363	int		mode,
364	struct nameidata *nd)
365{
366	return xfs_vn_mknod(dir, dentry, mode, 0);
367}
368
369STATIC int
370xfs_vn_mkdir(
371	struct inode	*dir,
372	struct dentry	*dentry,
373	int		mode)
374{
375	return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
376}
377
378STATIC struct dentry *
379xfs_vn_lookup(
380	struct inode	*dir,
381	struct dentry	*dentry,
382	struct nameidata *nd)
383{
384	bhv_vnode_t	*vp = vn_from_inode(dir), *cvp;
385	int		error;
386
387	if (dentry->d_name.len >= MAXNAMELEN)
388		return ERR_PTR(-ENAMETOOLONG);
389
390	error = bhv_vop_lookup(vp, dentry, &cvp, 0, NULL, NULL);
391	if (unlikely(error)) {
392		if (unlikely(error != ENOENT))
393			return ERR_PTR(-error);
394		d_add(dentry, NULL);
395		return NULL;
396	}
397
398	return d_splice_alias(vn_to_inode(cvp), dentry);
399}
400
401STATIC int
402xfs_vn_link(
403	struct dentry	*old_dentry,
404	struct inode	*dir,
405	struct dentry	*dentry)
406{
407	struct inode	*ip;	/* inode of guy being linked to */
408	bhv_vnode_t	*tdvp;	/* target directory for new name/link */
409	bhv_vnode_t	*vp;	/* vp of name being linked */
410	bhv_vattr_t	vattr;
411	int		error;
412
413	ip = old_dentry->d_inode;	/* inode being linked to */
414	tdvp = vn_from_inode(dir);
415	vp = vn_from_inode(ip);
416
417	VN_HOLD(vp);
418	error = bhv_vop_link(tdvp, vp, dentry, NULL);
419	if (unlikely(error)) {
420		VN_RELE(vp);
421	} else {
422		VMODIFY(tdvp);
423		xfs_validate_fields(ip, &vattr);
424		d_instantiate(dentry, ip);
425	}
426	return -error;
427}
428
429STATIC int
430xfs_vn_unlink(
431	struct inode	*dir,
432	struct dentry	*dentry)
433{
434	struct inode	*inode;
435	bhv_vnode_t	*dvp;	/* directory containing name to remove */
436	bhv_vattr_t	vattr;
437	int		error;
438
439	inode = dentry->d_inode;
440	dvp = vn_from_inode(dir);
441
442	error = bhv_vop_remove(dvp, dentry, NULL);
443	if (likely(!error)) {
444		xfs_validate_fields(dir, &vattr);	/* size needs update */
445		xfs_validate_fields(inode, &vattr);
446	}
447	return -error;
448}
449
450STATIC int
451xfs_vn_symlink(
452	struct inode	*dir,
453	struct dentry	*dentry,
454	const char	*symname)
455{
456	struct inode	*ip;
457	bhv_vattr_t	va = { 0 };
458	bhv_vnode_t	*dvp;	/* directory containing name of symlink */
459	bhv_vnode_t	*cvp;	/* used to lookup symlink to put in dentry */
460	int		error;
461
462	dvp = vn_from_inode(dir);
463	cvp = NULL;
464
465	va.va_mode = S_IFLNK |
466		(irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
467	va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
468
469	error = bhv_vop_symlink(dvp, dentry, &va, (char *)symname, &cvp, NULL);
470	if (likely(!error && cvp)) {
471		error = xfs_init_security(cvp, dir);
472		if (likely(!error)) {
473			ip = vn_to_inode(cvp);
474			d_instantiate(dentry, ip);
475			xfs_validate_fields(dir, &va);
476			xfs_validate_fields(ip, &va);
477		} else {
478			xfs_cleanup_inode(dvp, cvp, dentry, 0);
479		}
480	}
481	return -error;
482}
483
484STATIC int
485xfs_vn_rmdir(
486	struct inode	*dir,
487	struct dentry	*dentry)
488{
489	struct inode	*inode = dentry->d_inode;
490	bhv_vnode_t	*dvp = vn_from_inode(dir);
491	bhv_vattr_t	vattr;
492	int		error;
493
494	error = bhv_vop_rmdir(dvp, dentry, NULL);
495	if (likely(!error)) {
496		xfs_validate_fields(inode, &vattr);
497		xfs_validate_fields(dir, &vattr);
498	}
499	return -error;
500}
501
502STATIC int
503xfs_vn_rename(
504	struct inode	*odir,
505	struct dentry	*odentry,
506	struct inode	*ndir,
507	struct dentry	*ndentry)
508{
509	struct inode	*new_inode = ndentry->d_inode;
510	bhv_vnode_t	*fvp;	/* from directory */
511	bhv_vnode_t	*tvp;	/* target directory */
512	bhv_vattr_t	vattr;
513	int		error;
514
515	fvp = vn_from_inode(odir);
516	tvp = vn_from_inode(ndir);
517
518	error = bhv_vop_rename(fvp, odentry, tvp, ndentry, NULL);
519	if (likely(!error)) {
520		if (new_inode)
521			xfs_validate_fields(new_inode, &vattr);
522		xfs_validate_fields(odir, &vattr);
523		if (ndir != odir)
524			xfs_validate_fields(ndir, &vattr);
525	}
526	return -error;
527}
528
529/*
530 * careful here - this function can get called recursively, so
531 * we need to be very careful about how much stack we use.
532 * uio is kmalloced for this reason...
533 */
534STATIC void *
535xfs_vn_follow_link(
536	struct dentry		*dentry,
537	struct nameidata	*nd)
538{
539	bhv_vnode_t		*vp;
540	uio_t			*uio;
541	iovec_t			iov;
542	int			error;
543	char			*link;
544
545	ASSERT(dentry);
546	ASSERT(nd);
547
548	link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
549	if (!link) {
550		nd_set_link(nd, ERR_PTR(-ENOMEM));
551		return NULL;
552	}
553
554	uio = kmalloc(sizeof(uio_t), GFP_KERNEL);
555	if (!uio) {
556		kfree(link);
557		nd_set_link(nd, ERR_PTR(-ENOMEM));
558		return NULL;
559	}
560
561	vp = vn_from_inode(dentry->d_inode);
562
563	iov.iov_base = link;
564	iov.iov_len = MAXPATHLEN;
565
566	uio->uio_iov = &iov;
567	uio->uio_offset = 0;
568	uio->uio_segflg = UIO_SYSSPACE;
569	uio->uio_resid = MAXPATHLEN;
570	uio->uio_iovcnt = 1;
571
572	error = bhv_vop_readlink(vp, uio, 0, NULL);
573	if (unlikely(error)) {
574		kfree(link);
575		link = ERR_PTR(-error);
576	} else {
577		link[MAXPATHLEN - uio->uio_resid] = '\0';
578	}
579	kfree(uio);
580
581	nd_set_link(nd, link);
582	return NULL;
583}
584
585STATIC void
586xfs_vn_put_link(
587	struct dentry	*dentry,
588	struct nameidata *nd,
589	void		*p)
590{
591	char		*s = nd_get_link(nd);
592
593	if (!IS_ERR(s))
594		kfree(s);
595}
596
597#ifdef CONFIG_XFS_POSIX_ACL
598STATIC int
599xfs_vn_permission(
600	struct inode	*inode,
601	int		mode,
602	struct nameidata *nd)
603{
604	return -bhv_vop_access(vn_from_inode(inode), mode << 6, NULL);
605}
606#else
607#define xfs_vn_permission NULL
608#endif
609
610STATIC int
611xfs_vn_getattr(
612	struct vfsmount	*mnt,
613	struct dentry	*dentry,
614	struct kstat	*stat)
615{
616	struct inode	*inode = dentry->d_inode;
617	bhv_vnode_t	*vp = vn_from_inode(inode);
618	bhv_vattr_t	vattr = { .va_mask = XFS_AT_STAT };
619	int		error;
620
621	error = bhv_vop_getattr(vp, &vattr, ATTR_LAZY, NULL);
622	if (likely(!error)) {
623		stat->size = i_size_read(inode);
624		stat->dev = inode->i_sb->s_dev;
625		stat->rdev = (vattr.va_rdev == 0) ? 0 :
626				MKDEV(sysv_major(vattr.va_rdev) & 0x1ff,
627				      sysv_minor(vattr.va_rdev));
628		stat->mode = vattr.va_mode;
629		stat->nlink = vattr.va_nlink;
630		stat->uid = vattr.va_uid;
631		stat->gid = vattr.va_gid;
632		stat->ino = vattr.va_nodeid;
633		stat->atime = vattr.va_atime;
634		stat->mtime = vattr.va_mtime;
635		stat->ctime = vattr.va_ctime;
636		stat->blocks = vattr.va_nblocks;
637		stat->blksize = vattr.va_blocksize;
638	}
639	return -error;
640}
641
642STATIC int
643xfs_vn_setattr(
644	struct dentry	*dentry,
645	struct iattr	*attr)
646{
647	struct inode	*inode = dentry->d_inode;
648	unsigned int	ia_valid = attr->ia_valid;
649	bhv_vnode_t	*vp = vn_from_inode(inode);
650	bhv_vattr_t	vattr = { 0 };
651	int		flags = 0;
652	int		error;
653
654	if (ia_valid & ATTR_UID) {
655		vattr.va_mask |= XFS_AT_UID;
656		vattr.va_uid = attr->ia_uid;
657	}
658	if (ia_valid & ATTR_GID) {
659		vattr.va_mask |= XFS_AT_GID;
660		vattr.va_gid = attr->ia_gid;
661	}
662	if (ia_valid & ATTR_SIZE) {
663		vattr.va_mask |= XFS_AT_SIZE;
664		vattr.va_size = attr->ia_size;
665	}
666	if (ia_valid & ATTR_ATIME) {
667		vattr.va_mask |= XFS_AT_ATIME;
668		vattr.va_atime = attr->ia_atime;
669		inode->i_atime = attr->ia_atime;
670	}
671	if (ia_valid & ATTR_MTIME) {
672		vattr.va_mask |= XFS_AT_MTIME;
673		vattr.va_mtime = attr->ia_mtime;
674	}
675	if (ia_valid & ATTR_CTIME) {
676		vattr.va_mask |= XFS_AT_CTIME;
677		vattr.va_ctime = attr->ia_ctime;
678	}
679	if (ia_valid & ATTR_MODE) {
680		vattr.va_mask |= XFS_AT_MODE;
681		vattr.va_mode = attr->ia_mode;
682		if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
683			inode->i_mode &= ~S_ISGID;
684	}
685
686	if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
687		flags |= ATTR_UTIME;
688#ifdef ATTR_NO_BLOCK
689	if ((ia_valid & ATTR_NO_BLOCK))
690		flags |= ATTR_NONBLOCK;
691#endif
692
693	error = bhv_vop_setattr(vp, &vattr, flags, NULL);
694	if (likely(!error))
695		__vn_revalidate(vp, &vattr);
696	return -error;
697}
698
699STATIC void
700xfs_vn_truncate(
701	struct inode	*inode)
702{
703	block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
704}
705
706STATIC int
707xfs_vn_setxattr(
708	struct dentry	*dentry,
709	const char	*name,
710	const void	*data,
711	size_t		size,
712	int		flags)
713{
714	bhv_vnode_t	*vp = vn_from_inode(dentry->d_inode);
715	char		*attr = (char *)name;
716	attrnames_t	*namesp;
717	int		xflags = 0;
718	int		error;
719
720	namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
721	if (!namesp)
722		return -EOPNOTSUPP;
723	attr += namesp->attr_namelen;
724	error = namesp->attr_capable(vp, NULL);
725	if (error)
726		return error;
727
728	/* Convert Linux syscall to XFS internal ATTR flags */
729	if (flags & XATTR_CREATE)
730		xflags |= ATTR_CREATE;
731	if (flags & XATTR_REPLACE)
732		xflags |= ATTR_REPLACE;
733	xflags |= namesp->attr_flag;
734	return namesp->attr_set(vp, attr, (void *)data, size, xflags);
735}
736
737STATIC ssize_t
738xfs_vn_getxattr(
739	struct dentry	*dentry,
740	const char	*name,
741	void		*data,
742	size_t		size)
743{
744	bhv_vnode_t	*vp = vn_from_inode(dentry->d_inode);
745	char		*attr = (char *)name;
746	attrnames_t	*namesp;
747	int		xflags = 0;
748	ssize_t		error;
749
750	namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
751	if (!namesp)
752		return -EOPNOTSUPP;
753	attr += namesp->attr_namelen;
754	error = namesp->attr_capable(vp, NULL);
755	if (error)
756		return error;
757
758	/* Convert Linux syscall to XFS internal ATTR flags */
759	if (!size) {
760		xflags |= ATTR_KERNOVAL;
761		data = NULL;
762	}
763	xflags |= namesp->attr_flag;
764	return namesp->attr_get(vp, attr, (void *)data, size, xflags);
765}
766
767STATIC ssize_t
768xfs_vn_listxattr(
769	struct dentry		*dentry,
770	char			*data,
771	size_t			size)
772{
773	bhv_vnode_t		*vp = vn_from_inode(dentry->d_inode);
774	int			error, xflags = ATTR_KERNAMELS;
775	ssize_t			result;
776
777	if (!size)
778		xflags |= ATTR_KERNOVAL;
779	xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
780
781	error = attr_generic_list(vp, data, size, xflags, &result);
782	if (error < 0)
783		return error;
784	return result;
785}
786
787STATIC int
788xfs_vn_removexattr(
789	struct dentry	*dentry,
790	const char	*name)
791{
792	bhv_vnode_t	*vp = vn_from_inode(dentry->d_inode);
793	char		*attr = (char *)name;
794	attrnames_t	*namesp;
795	int		xflags = 0;
796	int		error;
797
798	namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
799	if (!namesp)
800		return -EOPNOTSUPP;
801	attr += namesp->attr_namelen;
802	error = namesp->attr_capable(vp, NULL);
803	if (error)
804		return error;
805	xflags |= namesp->attr_flag;
806	return namesp->attr_remove(vp, attr, xflags);
807}
808
809
810const struct inode_operations xfs_inode_operations = {
811	.permission		= xfs_vn_permission,
812	.truncate		= xfs_vn_truncate,
813	.getattr		= xfs_vn_getattr,
814	.setattr		= xfs_vn_setattr,
815	.setxattr		= xfs_vn_setxattr,
816	.getxattr		= xfs_vn_getxattr,
817	.listxattr		= xfs_vn_listxattr,
818	.removexattr		= xfs_vn_removexattr,
819};
820
821const struct inode_operations xfs_dir_inode_operations = {
822	.create			= xfs_vn_create,
823	.lookup			= xfs_vn_lookup,
824	.link			= xfs_vn_link,
825	.unlink			= xfs_vn_unlink,
826	.symlink		= xfs_vn_symlink,
827	.mkdir			= xfs_vn_mkdir,
828	.rmdir			= xfs_vn_rmdir,
829	.mknod			= xfs_vn_mknod,
830	.rename			= xfs_vn_rename,
831	.permission		= xfs_vn_permission,
832	.getattr		= xfs_vn_getattr,
833	.setattr		= xfs_vn_setattr,
834	.setxattr		= xfs_vn_setxattr,
835	.getxattr		= xfs_vn_getxattr,
836	.listxattr		= xfs_vn_listxattr,
837	.removexattr		= xfs_vn_removexattr,
838};
839
840const struct inode_operations xfs_symlink_inode_operations = {
841	.readlink		= generic_readlink,
842	.follow_link		= xfs_vn_follow_link,
843	.put_link		= xfs_vn_put_link,
844	.permission		= xfs_vn_permission,
845	.getattr		= xfs_vn_getattr,
846	.setattr		= xfs_vn_setattr,
847	.setxattr		= xfs_vn_setxattr,
848	.getxattr		= xfs_vn_getxattr,
849	.listxattr		= xfs_vn_listxattr,
850	.removexattr		= xfs_vn_removexattr,
851};
852