xfs_iops.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. #include "xfs_fs.h"
  34. #include "xfs_inum.h"
  35. #include "xfs_log.h"
  36. #include "xfs_trans.h"
  37. #include "xfs_sb.h"
  38. #include "xfs_ag.h"
  39. #include "xfs_dir.h"
  40. #include "xfs_dir2.h"
  41. #include "xfs_alloc.h"
  42. #include "xfs_dmapi.h"
  43. #include "xfs_quota.h"
  44. #include "xfs_mount.h"
  45. #include "xfs_alloc_btree.h"
  46. #include "xfs_bmap_btree.h"
  47. #include "xfs_ialloc_btree.h"
  48. #include "xfs_btree.h"
  49. #include "xfs_ialloc.h"
  50. #include "xfs_attr_sf.h"
  51. #include "xfs_dir_sf.h"
  52. #include "xfs_dir2_sf.h"
  53. #include "xfs_dinode.h"
  54. #include "xfs_inode.h"
  55. #include "xfs_bmap.h"
  56. #include "xfs_bit.h"
  57. #include "xfs_rtalloc.h"
  58. #include "xfs_error.h"
  59. #include "xfs_itable.h"
  60. #include "xfs_rw.h"
  61. #include "xfs_acl.h"
  62. #include "xfs_cap.h"
  63. #include "xfs_mac.h"
  64. #include "xfs_attr.h"
  65. #include "xfs_buf_item.h"
  66. #include "xfs_utils.h"
  67. #include <linux/xattr.h>
  68. #include <linux/namei.h>
  69. /*
  70. * Change the requested timestamp in the given inode.
  71. * We don't lock across timestamp updates, and we don't log them but
  72. * we do record the fact that there is dirty information in core.
  73. *
  74. * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
  75. * with XFS_ICHGTIME_ACC to be sure that access time
  76. * update will take. Calling first with XFS_ICHGTIME_ACC
  77. * and then XFS_ICHGTIME_MOD may fail to modify the access
  78. * timestamp if the filesystem is mounted noacctm.
  79. */
  80. void
  81. xfs_ichgtime(
  82. xfs_inode_t *ip,
  83. int flags)
  84. {
  85. struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
  86. timespec_t tv;
  87. /*
  88. * We're not supposed to change timestamps in readonly-mounted
  89. * filesystems. Throw it away if anyone asks us.
  90. */
  91. if (unlikely(IS_RDONLY(inode)))
  92. return;
  93. /*
  94. * Don't update access timestamps on reads if mounted "noatime".
  95. * Throw it away if anyone asks us.
  96. */
  97. if (unlikely(
  98. (ip->i_mount->m_flags & XFS_MOUNT_NOATIME || IS_NOATIME(inode)) &&
  99. (flags & (XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD|XFS_ICHGTIME_CHG)) ==
  100. XFS_ICHGTIME_ACC))
  101. return;
  102. nanotime(&tv);
  103. if (flags & XFS_ICHGTIME_MOD) {
  104. inode->i_mtime = tv;
  105. ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
  106. ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
  107. }
  108. if (flags & XFS_ICHGTIME_ACC) {
  109. inode->i_atime = tv;
  110. ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
  111. ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
  112. }
  113. if (flags & XFS_ICHGTIME_CHG) {
  114. inode->i_ctime = tv;
  115. ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
  116. ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
  117. }
  118. /*
  119. * We update the i_update_core field _after_ changing
  120. * the timestamps in order to coordinate properly with
  121. * xfs_iflush() so that we don't lose timestamp updates.
  122. * This keeps us from having to hold the inode lock
  123. * while doing this. We use the SYNCHRONIZE macro to
  124. * ensure that the compiler does not reorder the update
  125. * of i_update_core above the timestamp updates above.
  126. */
  127. SYNCHRONIZE();
  128. ip->i_update_core = 1;
  129. if (!(inode->i_state & I_LOCK))
  130. mark_inode_dirty_sync(inode);
  131. }
  132. /*
  133. * Variant on the above which avoids querying the system clock
  134. * in situations where we know the Linux inode timestamps have
  135. * just been updated (and so we can update our inode cheaply).
  136. * We also skip the readonly and noatime checks here, they are
  137. * also catered for already.
  138. */
  139. void
  140. xfs_ichgtime_fast(
  141. xfs_inode_t *ip,
  142. struct inode *inode,
  143. int flags)
  144. {
  145. timespec_t *tvp;
  146. /*
  147. * We're not supposed to change timestamps in readonly-mounted
  148. * filesystems. Throw it away if anyone asks us.
  149. */
  150. if (unlikely(IS_RDONLY(inode)))
  151. return;
  152. /*
  153. * Don't update access timestamps on reads if mounted "noatime".
  154. * Throw it away if anyone asks us.
  155. */
  156. if (unlikely(
  157. (ip->i_mount->m_flags & XFS_MOUNT_NOATIME || IS_NOATIME(inode)) &&
  158. ((flags & (XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD|XFS_ICHGTIME_CHG)) ==
  159. XFS_ICHGTIME_ACC)))
  160. return;
  161. if (flags & XFS_ICHGTIME_MOD) {
  162. tvp = &inode->i_mtime;
  163. ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
  164. ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
  165. }
  166. if (flags & XFS_ICHGTIME_ACC) {
  167. tvp = &inode->i_atime;
  168. ip->i_d.di_atime.t_sec = (__int32_t)tvp->tv_sec;
  169. ip->i_d.di_atime.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. * We update the i_update_core field _after_ changing
  178. * the timestamps in order to coordinate properly with
  179. * xfs_iflush() so that we don't lose timestamp updates.
  180. * This keeps us from having to hold the inode lock
  181. * while doing this. We use the SYNCHRONIZE macro to
  182. * ensure that the compiler does not reorder the update
  183. * of i_update_core above the timestamp updates above.
  184. */
  185. SYNCHRONIZE();
  186. ip->i_update_core = 1;
  187. if (!(inode->i_state & I_LOCK))
  188. mark_inode_dirty_sync(inode);
  189. }
  190. /*
  191. * Pull the link count and size up from the xfs inode to the linux inode
  192. */
  193. STATIC void
  194. validate_fields(
  195. struct inode *ip)
  196. {
  197. vnode_t *vp = LINVFS_GET_VP(ip);
  198. vattr_t va;
  199. int error;
  200. va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
  201. VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
  202. if (likely(!error)) {
  203. ip->i_nlink = va.va_nlink;
  204. ip->i_blocks = va.va_nblocks;
  205. /* we're under i_sem so i_size can't change under us */
  206. if (i_size_read(ip) != va.va_size)
  207. i_size_write(ip, va.va_size);
  208. }
  209. }
  210. /*
  211. * Determine whether a process has a valid fs_struct (kernel daemons
  212. * like knfsd don't have an fs_struct).
  213. *
  214. * XXX(hch): nfsd is broken, better fix it instead.
  215. */
  216. STATIC inline int
  217. has_fs_struct(struct task_struct *task)
  218. {
  219. return (task->fs != init_task.fs);
  220. }
  221. STATIC int
  222. linvfs_mknod(
  223. struct inode *dir,
  224. struct dentry *dentry,
  225. int mode,
  226. dev_t rdev)
  227. {
  228. struct inode *ip;
  229. vattr_t va;
  230. vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
  231. xfs_acl_t *default_acl = NULL;
  232. attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
  233. int error;
  234. /*
  235. * Irix uses Missed'em'V split, but doesn't want to see
  236. * the upper 5 bits of (14bit) major.
  237. */
  238. if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
  239. return -EINVAL;
  240. if (test_default_acl && test_default_acl(dvp)) {
  241. if (!_ACL_ALLOC(default_acl))
  242. return -ENOMEM;
  243. if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
  244. _ACL_FREE(default_acl);
  245. default_acl = NULL;
  246. }
  247. }
  248. if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
  249. mode &= ~current->fs->umask;
  250. memset(&va, 0, sizeof(va));
  251. va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
  252. va.va_mode = mode;
  253. switch (mode & S_IFMT) {
  254. case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
  255. va.va_rdev = sysv_encode_dev(rdev);
  256. va.va_mask |= XFS_AT_RDEV;
  257. /*FALLTHROUGH*/
  258. case S_IFREG:
  259. VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
  260. break;
  261. case S_IFDIR:
  262. VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
  263. break;
  264. default:
  265. error = EINVAL;
  266. break;
  267. }
  268. if (default_acl) {
  269. if (!error) {
  270. error = _ACL_INHERIT(vp, &va, default_acl);
  271. if (!error) {
  272. VMODIFY(vp);
  273. } else {
  274. struct dentry teardown = {};
  275. int err2;
  276. /* Oh, the horror.
  277. * If we can't add the ACL we must back out.
  278. * ENOSPC can hit here, among other things.
  279. */
  280. teardown.d_inode = ip = LINVFS_GET_IP(vp);
  281. teardown.d_name = dentry->d_name;
  282. vn_mark_bad(vp);
  283. if (S_ISDIR(mode))
  284. VOP_RMDIR(dvp, &teardown, NULL, err2);
  285. else
  286. VOP_REMOVE(dvp, &teardown, NULL, err2);
  287. VN_RELE(vp);
  288. }
  289. }
  290. _ACL_FREE(default_acl);
  291. }
  292. if (!error) {
  293. ASSERT(vp);
  294. ip = LINVFS_GET_IP(vp);
  295. if (S_ISCHR(mode) || S_ISBLK(mode))
  296. ip->i_rdev = rdev;
  297. else if (S_ISDIR(mode))
  298. validate_fields(ip);
  299. d_instantiate(dentry, ip);
  300. validate_fields(dir);
  301. }
  302. return -error;
  303. }
  304. STATIC int
  305. linvfs_create(
  306. struct inode *dir,
  307. struct dentry *dentry,
  308. int mode,
  309. struct nameidata *nd)
  310. {
  311. return linvfs_mknod(dir, dentry, mode, 0);
  312. }
  313. STATIC int
  314. linvfs_mkdir(
  315. struct inode *dir,
  316. struct dentry *dentry,
  317. int mode)
  318. {
  319. return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
  320. }
  321. STATIC struct dentry *
  322. linvfs_lookup(
  323. struct inode *dir,
  324. struct dentry *dentry,
  325. struct nameidata *nd)
  326. {
  327. struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
  328. int error;
  329. if (dentry->d_name.len >= MAXNAMELEN)
  330. return ERR_PTR(-ENAMETOOLONG);
  331. VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
  332. if (error) {
  333. if (unlikely(error != ENOENT))
  334. return ERR_PTR(-error);
  335. d_add(dentry, NULL);
  336. return NULL;
  337. }
  338. return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
  339. }
  340. STATIC int
  341. linvfs_link(
  342. struct dentry *old_dentry,
  343. struct inode *dir,
  344. struct dentry *dentry)
  345. {
  346. struct inode *ip; /* inode of guy being linked to */
  347. vnode_t *tdvp; /* target directory for new name/link */
  348. vnode_t *vp; /* vp of name being linked */
  349. int error;
  350. ip = old_dentry->d_inode; /* inode being linked to */
  351. if (S_ISDIR(ip->i_mode))
  352. return -EPERM;
  353. tdvp = LINVFS_GET_VP(dir);
  354. vp = LINVFS_GET_VP(ip);
  355. VOP_LINK(tdvp, vp, dentry, NULL, error);
  356. if (!error) {
  357. VMODIFY(tdvp);
  358. VN_HOLD(vp);
  359. validate_fields(ip);
  360. d_instantiate(dentry, ip);
  361. }
  362. return -error;
  363. }
  364. STATIC int
  365. linvfs_unlink(
  366. struct inode *dir,
  367. struct dentry *dentry)
  368. {
  369. struct inode *inode;
  370. vnode_t *dvp; /* directory containing name to remove */
  371. int error;
  372. inode = dentry->d_inode;
  373. dvp = LINVFS_GET_VP(dir);
  374. VOP_REMOVE(dvp, dentry, NULL, error);
  375. if (!error) {
  376. validate_fields(dir); /* For size only */
  377. validate_fields(inode);
  378. }
  379. return -error;
  380. }
  381. STATIC int
  382. linvfs_symlink(
  383. struct inode *dir,
  384. struct dentry *dentry,
  385. const char *symname)
  386. {
  387. struct inode *ip;
  388. vattr_t va;
  389. vnode_t *dvp; /* directory containing name of symlink */
  390. vnode_t *cvp; /* used to lookup symlink to put in dentry */
  391. int error;
  392. dvp = LINVFS_GET_VP(dir);
  393. cvp = NULL;
  394. memset(&va, 0, sizeof(va));
  395. va.va_mode = S_IFLNK |
  396. (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
  397. va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
  398. error = 0;
  399. VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
  400. if (!error && cvp) {
  401. ip = LINVFS_GET_IP(cvp);
  402. d_instantiate(dentry, ip);
  403. validate_fields(dir);
  404. validate_fields(ip); /* size needs update */
  405. }
  406. return -error;
  407. }
  408. STATIC int
  409. linvfs_rmdir(
  410. struct inode *dir,
  411. struct dentry *dentry)
  412. {
  413. struct inode *inode = dentry->d_inode;
  414. vnode_t *dvp = LINVFS_GET_VP(dir);
  415. int error;
  416. VOP_RMDIR(dvp, dentry, NULL, error);
  417. if (!error) {
  418. validate_fields(inode);
  419. validate_fields(dir);
  420. }
  421. return -error;
  422. }
  423. STATIC int
  424. linvfs_rename(
  425. struct inode *odir,
  426. struct dentry *odentry,
  427. struct inode *ndir,
  428. struct dentry *ndentry)
  429. {
  430. struct inode *new_inode = ndentry->d_inode;
  431. vnode_t *fvp; /* from directory */
  432. vnode_t *tvp; /* target directory */
  433. int error;
  434. fvp = LINVFS_GET_VP(odir);
  435. tvp = LINVFS_GET_VP(ndir);
  436. VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
  437. if (error)
  438. return -error;
  439. if (new_inode)
  440. validate_fields(new_inode);
  441. validate_fields(odir);
  442. if (ndir != odir)
  443. validate_fields(ndir);
  444. return 0;
  445. }
  446. /*
  447. * careful here - this function can get called recursively, so
  448. * we need to be very careful about how much stack we use.
  449. * uio is kmalloced for this reason...
  450. */
  451. STATIC void *
  452. linvfs_follow_link(
  453. struct dentry *dentry,
  454. struct nameidata *nd)
  455. {
  456. vnode_t *vp;
  457. uio_t *uio;
  458. iovec_t iov;
  459. int error;
  460. char *link;
  461. ASSERT(dentry);
  462. ASSERT(nd);
  463. link = (char *)kmalloc(MAXNAMELEN+1, GFP_KERNEL);
  464. if (!link) {
  465. nd_set_link(nd, ERR_PTR(-ENOMEM));
  466. return NULL;
  467. }
  468. uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
  469. if (!uio) {
  470. kfree(link);
  471. nd_set_link(nd, ERR_PTR(-ENOMEM));
  472. return NULL;
  473. }
  474. vp = LINVFS_GET_VP(dentry->d_inode);
  475. iov.iov_base = link;
  476. iov.iov_len = MAXNAMELEN;
  477. uio->uio_iov = &iov;
  478. uio->uio_offset = 0;
  479. uio->uio_segflg = UIO_SYSSPACE;
  480. uio->uio_resid = MAXNAMELEN;
  481. uio->uio_iovcnt = 1;
  482. VOP_READLINK(vp, uio, 0, NULL, error);
  483. if (error) {
  484. kfree(link);
  485. link = ERR_PTR(-error);
  486. } else {
  487. link[MAXNAMELEN - uio->uio_resid] = '\0';
  488. }
  489. kfree(uio);
  490. nd_set_link(nd, link);
  491. return NULL;
  492. }
  493. STATIC void
  494. linvfs_put_link(
  495. struct dentry *dentry,
  496. struct nameidata *nd,
  497. void *p)
  498. {
  499. char *s = nd_get_link(nd);
  500. if (!IS_ERR(s))
  501. kfree(s);
  502. }
  503. #ifdef CONFIG_XFS_POSIX_ACL
  504. STATIC int
  505. linvfs_permission(
  506. struct inode *inode,
  507. int mode,
  508. struct nameidata *nd)
  509. {
  510. vnode_t *vp = LINVFS_GET_VP(inode);
  511. int error;
  512. mode <<= 6; /* convert from linux to vnode access bits */
  513. VOP_ACCESS(vp, mode, NULL, error);
  514. return -error;
  515. }
  516. #else
  517. #define linvfs_permission NULL
  518. #endif
  519. STATIC int
  520. linvfs_getattr(
  521. struct vfsmount *mnt,
  522. struct dentry *dentry,
  523. struct kstat *stat)
  524. {
  525. struct inode *inode = dentry->d_inode;
  526. vnode_t *vp = LINVFS_GET_VP(inode);
  527. int error = 0;
  528. if (unlikely(vp->v_flag & VMODIFIED))
  529. error = vn_revalidate(vp);
  530. if (!error)
  531. generic_fillattr(inode, stat);
  532. return 0;
  533. }
  534. STATIC int
  535. linvfs_setattr(
  536. struct dentry *dentry,
  537. struct iattr *attr)
  538. {
  539. struct inode *inode = dentry->d_inode;
  540. unsigned int ia_valid = attr->ia_valid;
  541. vnode_t *vp = LINVFS_GET_VP(inode);
  542. vattr_t vattr;
  543. int flags = 0;
  544. int error;
  545. memset(&vattr, 0, sizeof(vattr_t));
  546. if (ia_valid & ATTR_UID) {
  547. vattr.va_mask |= XFS_AT_UID;
  548. vattr.va_uid = attr->ia_uid;
  549. }
  550. if (ia_valid & ATTR_GID) {
  551. vattr.va_mask |= XFS_AT_GID;
  552. vattr.va_gid = attr->ia_gid;
  553. }
  554. if (ia_valid & ATTR_SIZE) {
  555. vattr.va_mask |= XFS_AT_SIZE;
  556. vattr.va_size = attr->ia_size;
  557. }
  558. if (ia_valid & ATTR_ATIME) {
  559. vattr.va_mask |= XFS_AT_ATIME;
  560. vattr.va_atime = attr->ia_atime;
  561. }
  562. if (ia_valid & ATTR_MTIME) {
  563. vattr.va_mask |= XFS_AT_MTIME;
  564. vattr.va_mtime = attr->ia_mtime;
  565. }
  566. if (ia_valid & ATTR_CTIME) {
  567. vattr.va_mask |= XFS_AT_CTIME;
  568. vattr.va_ctime = attr->ia_ctime;
  569. }
  570. if (ia_valid & ATTR_MODE) {
  571. vattr.va_mask |= XFS_AT_MODE;
  572. vattr.va_mode = attr->ia_mode;
  573. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  574. inode->i_mode &= ~S_ISGID;
  575. }
  576. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
  577. flags |= ATTR_UTIME;
  578. #ifdef ATTR_NO_BLOCK
  579. if ((ia_valid & ATTR_NO_BLOCK))
  580. flags |= ATTR_NONBLOCK;
  581. #endif
  582. VOP_SETATTR(vp, &vattr, flags, NULL, error);
  583. if (error)
  584. return -error;
  585. vn_revalidate(vp);
  586. return error;
  587. }
  588. STATIC void
  589. linvfs_truncate(
  590. struct inode *inode)
  591. {
  592. block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
  593. }
  594. STATIC int
  595. linvfs_setxattr(
  596. struct dentry *dentry,
  597. const char *name,
  598. const void *data,
  599. size_t size,
  600. int flags)
  601. {
  602. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  603. char *attr = (char *)name;
  604. attrnames_t *namesp;
  605. int xflags = 0;
  606. int error;
  607. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  608. if (!namesp)
  609. return -EOPNOTSUPP;
  610. attr += namesp->attr_namelen;
  611. error = namesp->attr_capable(vp, NULL);
  612. if (error)
  613. return error;
  614. /* Convert Linux syscall to XFS internal ATTR flags */
  615. if (flags & XATTR_CREATE)
  616. xflags |= ATTR_CREATE;
  617. if (flags & XATTR_REPLACE)
  618. xflags |= ATTR_REPLACE;
  619. xflags |= namesp->attr_flag;
  620. return namesp->attr_set(vp, attr, (void *)data, size, xflags);
  621. }
  622. STATIC ssize_t
  623. linvfs_getxattr(
  624. struct dentry *dentry,
  625. const char *name,
  626. void *data,
  627. size_t size)
  628. {
  629. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  630. char *attr = (char *)name;
  631. attrnames_t *namesp;
  632. int xflags = 0;
  633. ssize_t error;
  634. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  635. if (!namesp)
  636. return -EOPNOTSUPP;
  637. attr += namesp->attr_namelen;
  638. error = namesp->attr_capable(vp, NULL);
  639. if (error)
  640. return error;
  641. /* Convert Linux syscall to XFS internal ATTR flags */
  642. if (!size) {
  643. xflags |= ATTR_KERNOVAL;
  644. data = NULL;
  645. }
  646. xflags |= namesp->attr_flag;
  647. return namesp->attr_get(vp, attr, (void *)data, size, xflags);
  648. }
  649. STATIC ssize_t
  650. linvfs_listxattr(
  651. struct dentry *dentry,
  652. char *data,
  653. size_t size)
  654. {
  655. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  656. int error, xflags = ATTR_KERNAMELS;
  657. ssize_t result;
  658. if (!size)
  659. xflags |= ATTR_KERNOVAL;
  660. xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
  661. error = attr_generic_list(vp, data, size, xflags, &result);
  662. if (error < 0)
  663. return error;
  664. return result;
  665. }
  666. STATIC int
  667. linvfs_removexattr(
  668. struct dentry *dentry,
  669. const char *name)
  670. {
  671. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  672. char *attr = (char *)name;
  673. attrnames_t *namesp;
  674. int xflags = 0;
  675. int error;
  676. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  677. if (!namesp)
  678. return -EOPNOTSUPP;
  679. attr += namesp->attr_namelen;
  680. error = namesp->attr_capable(vp, NULL);
  681. if (error)
  682. return error;
  683. xflags |= namesp->attr_flag;
  684. return namesp->attr_remove(vp, attr, xflags);
  685. }
  686. struct inode_operations linvfs_file_inode_operations = {
  687. .permission = linvfs_permission,
  688. .truncate = linvfs_truncate,
  689. .getattr = linvfs_getattr,
  690. .setattr = linvfs_setattr,
  691. .setxattr = linvfs_setxattr,
  692. .getxattr = linvfs_getxattr,
  693. .listxattr = linvfs_listxattr,
  694. .removexattr = linvfs_removexattr,
  695. };
  696. struct inode_operations linvfs_dir_inode_operations = {
  697. .create = linvfs_create,
  698. .lookup = linvfs_lookup,
  699. .link = linvfs_link,
  700. .unlink = linvfs_unlink,
  701. .symlink = linvfs_symlink,
  702. .mkdir = linvfs_mkdir,
  703. .rmdir = linvfs_rmdir,
  704. .mknod = linvfs_mknod,
  705. .rename = linvfs_rename,
  706. .permission = linvfs_permission,
  707. .getattr = linvfs_getattr,
  708. .setattr = linvfs_setattr,
  709. .setxattr = linvfs_setxattr,
  710. .getxattr = linvfs_getxattr,
  711. .listxattr = linvfs_listxattr,
  712. .removexattr = linvfs_removexattr,
  713. };
  714. struct inode_operations linvfs_symlink_inode_operations = {
  715. .readlink = generic_readlink,
  716. .follow_link = linvfs_follow_link,
  717. .put_link = linvfs_put_link,
  718. .permission = linvfs_permission,
  719. .getattr = linvfs_getattr,
  720. .setattr = linvfs_setattr,
  721. .setxattr = linvfs_setxattr,
  722. .getxattr = linvfs_getxattr,
  723. .listxattr = linvfs_listxattr,
  724. .removexattr = linvfs_removexattr,
  725. };