xfs_iops.c 18 KB

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