xfs_iops.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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_acl.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_quota.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_bmap.h"
  34. #include "xfs_rtalloc.h"
  35. #include "xfs_error.h"
  36. #include "xfs_itable.h"
  37. #include "xfs_rw.h"
  38. #include "xfs_attr.h"
  39. #include "xfs_buf_item.h"
  40. #include "xfs_utils.h"
  41. #include "xfs_vnodeops.h"
  42. #include "xfs_inode_item.h"
  43. #include "xfs_trace.h"
  44. #include <linux/capability.h>
  45. #include <linux/xattr.h>
  46. #include <linux/namei.h>
  47. #include <linux/posix_acl.h>
  48. #include <linux/security.h>
  49. #include <linux/fiemap.h>
  50. #include <linux/slab.h>
  51. /*
  52. * Bring the timestamps in the XFS inode uptodate.
  53. *
  54. * Used before writing the inode to disk.
  55. */
  56. void
  57. xfs_synchronize_times(
  58. xfs_inode_t *ip)
  59. {
  60. struct inode *inode = VFS_I(ip);
  61. ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
  62. ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
  63. ip->i_d.di_ctime.t_sec = (__int32_t)inode->i_ctime.tv_sec;
  64. ip->i_d.di_ctime.t_nsec = (__int32_t)inode->i_ctime.tv_nsec;
  65. ip->i_d.di_mtime.t_sec = (__int32_t)inode->i_mtime.tv_sec;
  66. ip->i_d.di_mtime.t_nsec = (__int32_t)inode->i_mtime.tv_nsec;
  67. }
  68. /*
  69. * If the linux inode is valid, mark it dirty, else mark the dirty state
  70. * in the XFS inode to make sure we pick it up when reclaiming the inode.
  71. */
  72. void
  73. xfs_mark_inode_dirty_sync(
  74. xfs_inode_t *ip)
  75. {
  76. struct inode *inode = VFS_I(ip);
  77. if (!(inode->i_state & (I_WILL_FREE|I_FREEING)))
  78. mark_inode_dirty_sync(inode);
  79. else {
  80. barrier();
  81. ip->i_update_core = 1;
  82. }
  83. }
  84. void
  85. xfs_mark_inode_dirty(
  86. xfs_inode_t *ip)
  87. {
  88. struct inode *inode = VFS_I(ip);
  89. if (!(inode->i_state & (I_WILL_FREE|I_FREEING)))
  90. mark_inode_dirty(inode);
  91. else {
  92. barrier();
  93. ip->i_update_core = 1;
  94. }
  95. }
  96. int xfs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  97. void *fs_info)
  98. {
  99. const struct xattr *xattr;
  100. struct xfs_inode *ip = XFS_I(inode);
  101. int error = 0;
  102. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  103. error = xfs_attr_set(ip, xattr->name, xattr->value,
  104. xattr->value_len, ATTR_SECURE);
  105. if (error < 0)
  106. break;
  107. }
  108. return error;
  109. }
  110. /*
  111. * Hook in SELinux. This is not quite correct yet, what we really need
  112. * here (as we do for default ACLs) is a mechanism by which creation of
  113. * these attrs can be journalled at inode creation time (along with the
  114. * inode, of course, such that log replay can't cause these to be lost).
  115. */
  116. STATIC int
  117. xfs_init_security(
  118. struct inode *inode,
  119. struct inode *dir,
  120. const struct qstr *qstr)
  121. {
  122. return security_inode_init_security(inode, dir, qstr,
  123. &xfs_initxattrs, NULL);
  124. }
  125. static void
  126. xfs_dentry_to_name(
  127. struct xfs_name *namep,
  128. struct dentry *dentry)
  129. {
  130. namep->name = dentry->d_name.name;
  131. namep->len = dentry->d_name.len;
  132. }
  133. STATIC void
  134. xfs_cleanup_inode(
  135. struct inode *dir,
  136. struct inode *inode,
  137. struct dentry *dentry)
  138. {
  139. struct xfs_name teardown;
  140. /* Oh, the horror.
  141. * If we can't add the ACL or we fail in
  142. * xfs_init_security we must back out.
  143. * ENOSPC can hit here, among other things.
  144. */
  145. xfs_dentry_to_name(&teardown, dentry);
  146. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  147. iput(inode);
  148. }
  149. STATIC int
  150. xfs_vn_mknod(
  151. struct inode *dir,
  152. struct dentry *dentry,
  153. umode_t mode,
  154. dev_t rdev)
  155. {
  156. struct inode *inode;
  157. struct xfs_inode *ip = NULL;
  158. struct posix_acl *default_acl = NULL;
  159. struct xfs_name name;
  160. int error;
  161. /*
  162. * Irix uses Missed'em'V split, but doesn't want to see
  163. * the upper 5 bits of (14bit) major.
  164. */
  165. if (S_ISCHR(mode) || S_ISBLK(mode)) {
  166. if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
  167. return -EINVAL;
  168. rdev = sysv_encode_dev(rdev);
  169. } else {
  170. rdev = 0;
  171. }
  172. if (IS_POSIXACL(dir)) {
  173. default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
  174. if (IS_ERR(default_acl))
  175. return PTR_ERR(default_acl);
  176. if (!default_acl)
  177. mode &= ~current_umask();
  178. }
  179. xfs_dentry_to_name(&name, dentry);
  180. error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
  181. if (unlikely(error))
  182. goto out_free_acl;
  183. inode = VFS_I(ip);
  184. error = xfs_init_security(inode, dir, &dentry->d_name);
  185. if (unlikely(error))
  186. goto out_cleanup_inode;
  187. if (default_acl) {
  188. error = -xfs_inherit_acl(inode, default_acl);
  189. default_acl = NULL;
  190. if (unlikely(error))
  191. goto out_cleanup_inode;
  192. }
  193. d_instantiate(dentry, inode);
  194. return -error;
  195. out_cleanup_inode:
  196. xfs_cleanup_inode(dir, inode, dentry);
  197. out_free_acl:
  198. posix_acl_release(default_acl);
  199. return -error;
  200. }
  201. STATIC int
  202. xfs_vn_create(
  203. struct inode *dir,
  204. struct dentry *dentry,
  205. umode_t mode,
  206. struct nameidata *nd)
  207. {
  208. return xfs_vn_mknod(dir, dentry, mode, 0);
  209. }
  210. STATIC int
  211. xfs_vn_mkdir(
  212. struct inode *dir,
  213. struct dentry *dentry,
  214. umode_t mode)
  215. {
  216. return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
  217. }
  218. STATIC struct dentry *
  219. xfs_vn_lookup(
  220. struct inode *dir,
  221. struct dentry *dentry,
  222. struct nameidata *nd)
  223. {
  224. struct xfs_inode *cip;
  225. struct xfs_name name;
  226. int error;
  227. if (dentry->d_name.len >= MAXNAMELEN)
  228. return ERR_PTR(-ENAMETOOLONG);
  229. xfs_dentry_to_name(&name, dentry);
  230. error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
  231. if (unlikely(error)) {
  232. if (unlikely(error != ENOENT))
  233. return ERR_PTR(-error);
  234. d_add(dentry, NULL);
  235. return NULL;
  236. }
  237. return d_splice_alias(VFS_I(cip), dentry);
  238. }
  239. STATIC struct dentry *
  240. xfs_vn_ci_lookup(
  241. struct inode *dir,
  242. struct dentry *dentry,
  243. struct nameidata *nd)
  244. {
  245. struct xfs_inode *ip;
  246. struct xfs_name xname;
  247. struct xfs_name ci_name;
  248. struct qstr dname;
  249. int error;
  250. if (dentry->d_name.len >= MAXNAMELEN)
  251. return ERR_PTR(-ENAMETOOLONG);
  252. xfs_dentry_to_name(&xname, dentry);
  253. error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
  254. if (unlikely(error)) {
  255. if (unlikely(error != ENOENT))
  256. return ERR_PTR(-error);
  257. /*
  258. * call d_add(dentry, NULL) here when d_drop_negative_children
  259. * is called in xfs_vn_mknod (ie. allow negative dentries
  260. * with CI filesystems).
  261. */
  262. return NULL;
  263. }
  264. /* if exact match, just splice and exit */
  265. if (!ci_name.name)
  266. return d_splice_alias(VFS_I(ip), dentry);
  267. /* else case-insensitive match... */
  268. dname.name = ci_name.name;
  269. dname.len = ci_name.len;
  270. dentry = d_add_ci(dentry, VFS_I(ip), &dname);
  271. kmem_free(ci_name.name);
  272. return dentry;
  273. }
  274. STATIC int
  275. xfs_vn_link(
  276. struct dentry *old_dentry,
  277. struct inode *dir,
  278. struct dentry *dentry)
  279. {
  280. struct inode *inode = old_dentry->d_inode;
  281. struct xfs_name name;
  282. int error;
  283. xfs_dentry_to_name(&name, dentry);
  284. error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
  285. if (unlikely(error))
  286. return -error;
  287. ihold(inode);
  288. d_instantiate(dentry, inode);
  289. return 0;
  290. }
  291. STATIC int
  292. xfs_vn_unlink(
  293. struct inode *dir,
  294. struct dentry *dentry)
  295. {
  296. struct xfs_name name;
  297. int error;
  298. xfs_dentry_to_name(&name, dentry);
  299. error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
  300. if (error)
  301. return error;
  302. /*
  303. * With unlink, the VFS makes the dentry "negative": no inode,
  304. * but still hashed. This is incompatible with case-insensitive
  305. * mode, so invalidate (unhash) the dentry in CI-mode.
  306. */
  307. if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
  308. d_invalidate(dentry);
  309. return 0;
  310. }
  311. STATIC int
  312. xfs_vn_symlink(
  313. struct inode *dir,
  314. struct dentry *dentry,
  315. const char *symname)
  316. {
  317. struct inode *inode;
  318. struct xfs_inode *cip = NULL;
  319. struct xfs_name name;
  320. int error;
  321. umode_t mode;
  322. mode = S_IFLNK |
  323. (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
  324. xfs_dentry_to_name(&name, dentry);
  325. error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
  326. if (unlikely(error))
  327. goto out;
  328. inode = VFS_I(cip);
  329. error = xfs_init_security(inode, dir, &dentry->d_name);
  330. if (unlikely(error))
  331. goto out_cleanup_inode;
  332. d_instantiate(dentry, inode);
  333. return 0;
  334. out_cleanup_inode:
  335. xfs_cleanup_inode(dir, inode, dentry);
  336. out:
  337. return -error;
  338. }
  339. STATIC int
  340. xfs_vn_rename(
  341. struct inode *odir,
  342. struct dentry *odentry,
  343. struct inode *ndir,
  344. struct dentry *ndentry)
  345. {
  346. struct inode *new_inode = ndentry->d_inode;
  347. struct xfs_name oname;
  348. struct xfs_name nname;
  349. xfs_dentry_to_name(&oname, odentry);
  350. xfs_dentry_to_name(&nname, ndentry);
  351. return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
  352. XFS_I(ndir), &nname, new_inode ?
  353. XFS_I(new_inode) : NULL);
  354. }
  355. /*
  356. * careful here - this function can get called recursively, so
  357. * we need to be very careful about how much stack we use.
  358. * uio is kmalloced for this reason...
  359. */
  360. STATIC void *
  361. xfs_vn_follow_link(
  362. struct dentry *dentry,
  363. struct nameidata *nd)
  364. {
  365. char *link;
  366. int error = -ENOMEM;
  367. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  368. if (!link)
  369. goto out_err;
  370. error = -xfs_readlink(XFS_I(dentry->d_inode), link);
  371. if (unlikely(error))
  372. goto out_kfree;
  373. nd_set_link(nd, link);
  374. return NULL;
  375. out_kfree:
  376. kfree(link);
  377. out_err:
  378. nd_set_link(nd, ERR_PTR(error));
  379. return NULL;
  380. }
  381. STATIC void
  382. xfs_vn_put_link(
  383. struct dentry *dentry,
  384. struct nameidata *nd,
  385. void *p)
  386. {
  387. char *s = nd_get_link(nd);
  388. if (!IS_ERR(s))
  389. kfree(s);
  390. }
  391. STATIC int
  392. xfs_vn_getattr(
  393. struct vfsmount *mnt,
  394. struct dentry *dentry,
  395. struct kstat *stat)
  396. {
  397. struct inode *inode = dentry->d_inode;
  398. struct xfs_inode *ip = XFS_I(inode);
  399. struct xfs_mount *mp = ip->i_mount;
  400. trace_xfs_getattr(ip);
  401. if (XFS_FORCED_SHUTDOWN(mp))
  402. return -XFS_ERROR(EIO);
  403. stat->size = XFS_ISIZE(ip);
  404. stat->dev = inode->i_sb->s_dev;
  405. stat->mode = ip->i_d.di_mode;
  406. stat->nlink = ip->i_d.di_nlink;
  407. stat->uid = ip->i_d.di_uid;
  408. stat->gid = ip->i_d.di_gid;
  409. stat->ino = ip->i_ino;
  410. stat->atime = inode->i_atime;
  411. stat->mtime = inode->i_mtime;
  412. stat->ctime = inode->i_ctime;
  413. stat->blocks =
  414. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  415. switch (inode->i_mode & S_IFMT) {
  416. case S_IFBLK:
  417. case S_IFCHR:
  418. stat->blksize = BLKDEV_IOSIZE;
  419. stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  420. sysv_minor(ip->i_df.if_u2.if_rdev));
  421. break;
  422. default:
  423. if (XFS_IS_REALTIME_INODE(ip)) {
  424. /*
  425. * If the file blocks are being allocated from a
  426. * realtime volume, then return the inode's realtime
  427. * extent size or the realtime volume's extent size.
  428. */
  429. stat->blksize =
  430. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  431. } else
  432. stat->blksize = xfs_preferred_iosize(mp);
  433. stat->rdev = 0;
  434. break;
  435. }
  436. return 0;
  437. }
  438. int
  439. xfs_setattr_nonsize(
  440. struct xfs_inode *ip,
  441. struct iattr *iattr,
  442. int flags)
  443. {
  444. xfs_mount_t *mp = ip->i_mount;
  445. struct inode *inode = VFS_I(ip);
  446. int mask = iattr->ia_valid;
  447. xfs_trans_t *tp;
  448. int error;
  449. uid_t uid = 0, iuid = 0;
  450. gid_t gid = 0, igid = 0;
  451. struct xfs_dquot *udqp = NULL, *gdqp = NULL;
  452. struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
  453. trace_xfs_setattr(ip);
  454. if (mp->m_flags & XFS_MOUNT_RDONLY)
  455. return XFS_ERROR(EROFS);
  456. if (XFS_FORCED_SHUTDOWN(mp))
  457. return XFS_ERROR(EIO);
  458. error = -inode_change_ok(inode, iattr);
  459. if (error)
  460. return XFS_ERROR(error);
  461. ASSERT((mask & ATTR_SIZE) == 0);
  462. /*
  463. * If disk quotas is on, we make sure that the dquots do exist on disk,
  464. * before we start any other transactions. Trying to do this later
  465. * is messy. We don't care to take a readlock to look at the ids
  466. * in inode here, because we can't hold it across the trans_reserve.
  467. * If the IDs do change before we take the ilock, we're covered
  468. * because the i_*dquot fields will get updated anyway.
  469. */
  470. if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
  471. uint qflags = 0;
  472. if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
  473. uid = iattr->ia_uid;
  474. qflags |= XFS_QMOPT_UQUOTA;
  475. } else {
  476. uid = ip->i_d.di_uid;
  477. }
  478. if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
  479. gid = iattr->ia_gid;
  480. qflags |= XFS_QMOPT_GQUOTA;
  481. } else {
  482. gid = ip->i_d.di_gid;
  483. }
  484. /*
  485. * We take a reference when we initialize udqp and gdqp,
  486. * so it is important that we never blindly double trip on
  487. * the same variable. See xfs_create() for an example.
  488. */
  489. ASSERT(udqp == NULL);
  490. ASSERT(gdqp == NULL);
  491. error = xfs_qm_vop_dqalloc(ip, uid, gid, xfs_get_projid(ip),
  492. qflags, &udqp, &gdqp);
  493. if (error)
  494. return error;
  495. }
  496. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  497. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  498. if (error)
  499. goto out_dqrele;
  500. xfs_ilock(ip, XFS_ILOCK_EXCL);
  501. /*
  502. * Change file ownership. Must be the owner or privileged.
  503. */
  504. if (mask & (ATTR_UID|ATTR_GID)) {
  505. /*
  506. * These IDs could have changed since we last looked at them.
  507. * But, we're assured that if the ownership did change
  508. * while we didn't have the inode locked, inode's dquot(s)
  509. * would have changed also.
  510. */
  511. iuid = ip->i_d.di_uid;
  512. igid = ip->i_d.di_gid;
  513. gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
  514. uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
  515. /*
  516. * Do a quota reservation only if uid/gid is actually
  517. * going to change.
  518. */
  519. if (XFS_IS_QUOTA_RUNNING(mp) &&
  520. ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
  521. (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
  522. ASSERT(tp);
  523. error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  524. capable(CAP_FOWNER) ?
  525. XFS_QMOPT_FORCE_RES : 0);
  526. if (error) /* out of quota */
  527. goto out_trans_cancel;
  528. }
  529. }
  530. xfs_trans_ijoin(tp, ip, 0);
  531. /*
  532. * Change file ownership. Must be the owner or privileged.
  533. */
  534. if (mask & (ATTR_UID|ATTR_GID)) {
  535. /*
  536. * CAP_FSETID overrides the following restrictions:
  537. *
  538. * The set-user-ID and set-group-ID bits of a file will be
  539. * cleared upon successful return from chown()
  540. */
  541. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  542. !capable(CAP_FSETID))
  543. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  544. /*
  545. * Change the ownerships and register quota modifications
  546. * in the transaction.
  547. */
  548. if (iuid != uid) {
  549. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
  550. ASSERT(mask & ATTR_UID);
  551. ASSERT(udqp);
  552. olddquot1 = xfs_qm_vop_chown(tp, ip,
  553. &ip->i_udquot, udqp);
  554. }
  555. ip->i_d.di_uid = uid;
  556. inode->i_uid = uid;
  557. }
  558. if (igid != gid) {
  559. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
  560. ASSERT(!XFS_IS_PQUOTA_ON(mp));
  561. ASSERT(mask & ATTR_GID);
  562. ASSERT(gdqp);
  563. olddquot2 = xfs_qm_vop_chown(tp, ip,
  564. &ip->i_gdquot, gdqp);
  565. }
  566. ip->i_d.di_gid = gid;
  567. inode->i_gid = gid;
  568. }
  569. }
  570. /*
  571. * Change file access modes.
  572. */
  573. if (mask & ATTR_MODE) {
  574. umode_t mode = iattr->ia_mode;
  575. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  576. mode &= ~S_ISGID;
  577. ip->i_d.di_mode &= S_IFMT;
  578. ip->i_d.di_mode |= mode & ~S_IFMT;
  579. inode->i_mode &= S_IFMT;
  580. inode->i_mode |= mode & ~S_IFMT;
  581. }
  582. /*
  583. * Change file access or modified times.
  584. */
  585. if (mask & ATTR_ATIME) {
  586. inode->i_atime = iattr->ia_atime;
  587. ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
  588. ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
  589. ip->i_update_core = 1;
  590. }
  591. if (mask & ATTR_CTIME) {
  592. inode->i_ctime = iattr->ia_ctime;
  593. ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
  594. ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
  595. ip->i_update_core = 1;
  596. }
  597. if (mask & ATTR_MTIME) {
  598. inode->i_mtime = iattr->ia_mtime;
  599. ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
  600. ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
  601. ip->i_update_core = 1;
  602. }
  603. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  604. XFS_STATS_INC(xs_ig_attrchg);
  605. if (mp->m_flags & XFS_MOUNT_WSYNC)
  606. xfs_trans_set_sync(tp);
  607. error = xfs_trans_commit(tp, 0);
  608. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  609. /*
  610. * Release any dquot(s) the inode had kept before chown.
  611. */
  612. xfs_qm_dqrele(olddquot1);
  613. xfs_qm_dqrele(olddquot2);
  614. xfs_qm_dqrele(udqp);
  615. xfs_qm_dqrele(gdqp);
  616. if (error)
  617. return XFS_ERROR(error);
  618. /*
  619. * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
  620. * update. We could avoid this with linked transactions
  621. * and passing down the transaction pointer all the way
  622. * to attr_set. No previous user of the generic
  623. * Posix ACL code seems to care about this issue either.
  624. */
  625. if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
  626. error = -xfs_acl_chmod(inode);
  627. if (error)
  628. return XFS_ERROR(error);
  629. }
  630. return 0;
  631. out_trans_cancel:
  632. xfs_trans_cancel(tp, 0);
  633. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  634. out_dqrele:
  635. xfs_qm_dqrele(udqp);
  636. xfs_qm_dqrele(gdqp);
  637. return error;
  638. }
  639. /*
  640. * Truncate file. Must have write permission and not be a directory.
  641. */
  642. int
  643. xfs_setattr_size(
  644. struct xfs_inode *ip,
  645. struct iattr *iattr,
  646. int flags)
  647. {
  648. struct xfs_mount *mp = ip->i_mount;
  649. struct inode *inode = VFS_I(ip);
  650. int mask = iattr->ia_valid;
  651. xfs_off_t oldsize, newsize;
  652. struct xfs_trans *tp;
  653. int error;
  654. uint lock_flags;
  655. uint commit_flags = 0;
  656. trace_xfs_setattr(ip);
  657. if (mp->m_flags & XFS_MOUNT_RDONLY)
  658. return XFS_ERROR(EROFS);
  659. if (XFS_FORCED_SHUTDOWN(mp))
  660. return XFS_ERROR(EIO);
  661. error = -inode_change_ok(inode, iattr);
  662. if (error)
  663. return XFS_ERROR(error);
  664. ASSERT(S_ISREG(ip->i_d.di_mode));
  665. ASSERT((mask & (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
  666. ATTR_MTIME_SET|ATTR_KILL_SUID|ATTR_KILL_SGID|
  667. ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
  668. lock_flags = XFS_ILOCK_EXCL;
  669. if (!(flags & XFS_ATTR_NOLOCK))
  670. lock_flags |= XFS_IOLOCK_EXCL;
  671. xfs_ilock(ip, lock_flags);
  672. oldsize = ip->i_size;
  673. newsize = iattr->ia_size;
  674. /*
  675. * Short circuit the truncate case for zero length files.
  676. */
  677. if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
  678. if (!(mask & (ATTR_CTIME|ATTR_MTIME)))
  679. goto out_unlock;
  680. /*
  681. * Use the regular setattr path to update the timestamps.
  682. */
  683. xfs_iunlock(ip, lock_flags);
  684. iattr->ia_valid &= ~ATTR_SIZE;
  685. return xfs_setattr_nonsize(ip, iattr, 0);
  686. }
  687. /*
  688. * Make sure that the dquots are attached to the inode.
  689. */
  690. error = xfs_qm_dqattach_locked(ip, 0);
  691. if (error)
  692. goto out_unlock;
  693. /*
  694. * Now we can make the changes. Before we join the inode to the
  695. * transaction, take care of the part of the truncation that must be
  696. * done without the inode lock. This needs to be done before joining
  697. * the inode to the transaction, because the inode cannot be unlocked
  698. * once it is a part of the transaction.
  699. */
  700. if (newsize > oldsize) {
  701. /*
  702. * Do the first part of growing a file: zero any data in the
  703. * last block that is beyond the old EOF. We need to do this
  704. * before the inode is joined to the transaction to modify
  705. * i_size.
  706. */
  707. error = xfs_zero_eof(ip, newsize, oldsize);
  708. if (error)
  709. goto out_unlock;
  710. }
  711. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  712. lock_flags &= ~XFS_ILOCK_EXCL;
  713. /*
  714. * We are going to log the inode size change in this transaction so
  715. * any previous writes that are beyond the on disk EOF and the new
  716. * EOF that have not been written out need to be written here. If we
  717. * do not write the data out, we expose ourselves to the null files
  718. * problem.
  719. *
  720. * Only flush from the on disk size to the smaller of the in memory
  721. * file size or the new size as that's the range we really care about
  722. * here and prevents waiting for other data not within the range we
  723. * care about here.
  724. */
  725. if (oldsize != ip->i_d.di_size && newsize > ip->i_d.di_size) {
  726. error = xfs_flush_pages(ip, ip->i_d.di_size, newsize, 0,
  727. FI_NONE);
  728. if (error)
  729. goto out_unlock;
  730. }
  731. /*
  732. * Wait for all direct I/O to complete.
  733. */
  734. inode_dio_wait(inode);
  735. error = -block_truncate_page(inode->i_mapping, newsize, xfs_get_blocks);
  736. if (error)
  737. goto out_unlock;
  738. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
  739. error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  740. XFS_TRANS_PERM_LOG_RES,
  741. XFS_ITRUNCATE_LOG_COUNT);
  742. if (error)
  743. goto out_trans_cancel;
  744. truncate_setsize(inode, newsize);
  745. commit_flags = XFS_TRANS_RELEASE_LOG_RES;
  746. lock_flags |= XFS_ILOCK_EXCL;
  747. xfs_ilock(ip, XFS_ILOCK_EXCL);
  748. xfs_trans_ijoin(tp, ip, 0);
  749. /*
  750. * Only change the c/mtime if we are changing the size or we are
  751. * explicitly asked to change it. This handles the semantic difference
  752. * between truncate() and ftruncate() as implemented in the VFS.
  753. *
  754. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  755. * special case where we need to update the times despite not having
  756. * these flags set. For all other operations the VFS set these flags
  757. * explicitly if it wants a timestamp update.
  758. */
  759. if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) {
  760. iattr->ia_ctime = iattr->ia_mtime =
  761. current_fs_time(inode->i_sb);
  762. mask |= ATTR_CTIME | ATTR_MTIME;
  763. }
  764. /*
  765. * The first thing we do is set the size to new_size permanently on
  766. * disk. This way we don't have to worry about anyone ever being able
  767. * to look at the data being freed even in the face of a crash.
  768. * What we're getting around here is the case where we free a block, it
  769. * is allocated to another file, it is written to, and then we crash.
  770. * If the new data gets written to the file but the log buffers
  771. * containing the free and reallocation don't, then we'd end up with
  772. * garbage in the blocks being freed. As long as we make the new size
  773. * permanent before actually freeing any blocks it doesn't matter if
  774. * they get written to.
  775. */
  776. ip->i_d.di_size = newsize;
  777. ip->i_size = newsize;
  778. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  779. if (newsize <= oldsize) {
  780. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
  781. if (error)
  782. goto out_trans_abort;
  783. /*
  784. * Truncated "down", so we're removing references to old data
  785. * here - if we delay flushing for a long time, we expose
  786. * ourselves unduly to the notorious NULL files problem. So,
  787. * we mark this inode and flush it when the file is closed,
  788. * and do not wait the usual (long) time for writeout.
  789. */
  790. xfs_iflags_set(ip, XFS_ITRUNCATED);
  791. }
  792. if (mask & ATTR_CTIME) {
  793. inode->i_ctime = iattr->ia_ctime;
  794. ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
  795. ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
  796. ip->i_update_core = 1;
  797. }
  798. if (mask & ATTR_MTIME) {
  799. inode->i_mtime = iattr->ia_mtime;
  800. ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
  801. ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
  802. ip->i_update_core = 1;
  803. }
  804. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  805. XFS_STATS_INC(xs_ig_attrchg);
  806. if (mp->m_flags & XFS_MOUNT_WSYNC)
  807. xfs_trans_set_sync(tp);
  808. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  809. out_unlock:
  810. if (lock_flags)
  811. xfs_iunlock(ip, lock_flags);
  812. return error;
  813. out_trans_abort:
  814. commit_flags |= XFS_TRANS_ABORT;
  815. out_trans_cancel:
  816. xfs_trans_cancel(tp, commit_flags);
  817. goto out_unlock;
  818. }
  819. STATIC int
  820. xfs_vn_setattr(
  821. struct dentry *dentry,
  822. struct iattr *iattr)
  823. {
  824. if (iattr->ia_valid & ATTR_SIZE)
  825. return -xfs_setattr_size(XFS_I(dentry->d_inode), iattr, 0);
  826. return -xfs_setattr_nonsize(XFS_I(dentry->d_inode), iattr, 0);
  827. }
  828. #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  829. /*
  830. * Call fiemap helper to fill in user data.
  831. * Returns positive errors to xfs_getbmap.
  832. */
  833. STATIC int
  834. xfs_fiemap_format(
  835. void **arg,
  836. struct getbmapx *bmv,
  837. int *full)
  838. {
  839. int error;
  840. struct fiemap_extent_info *fieinfo = *arg;
  841. u32 fiemap_flags = 0;
  842. u64 logical, physical, length;
  843. /* Do nothing for a hole */
  844. if (bmv->bmv_block == -1LL)
  845. return 0;
  846. logical = BBTOB(bmv->bmv_offset);
  847. physical = BBTOB(bmv->bmv_block);
  848. length = BBTOB(bmv->bmv_length);
  849. if (bmv->bmv_oflags & BMV_OF_PREALLOC)
  850. fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
  851. else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
  852. fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
  853. physical = 0; /* no block yet */
  854. }
  855. if (bmv->bmv_oflags & BMV_OF_LAST)
  856. fiemap_flags |= FIEMAP_EXTENT_LAST;
  857. error = fiemap_fill_next_extent(fieinfo, logical, physical,
  858. length, fiemap_flags);
  859. if (error > 0) {
  860. error = 0;
  861. *full = 1; /* user array now full */
  862. }
  863. return -error;
  864. }
  865. STATIC int
  866. xfs_vn_fiemap(
  867. struct inode *inode,
  868. struct fiemap_extent_info *fieinfo,
  869. u64 start,
  870. u64 length)
  871. {
  872. xfs_inode_t *ip = XFS_I(inode);
  873. struct getbmapx bm;
  874. int error;
  875. error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
  876. if (error)
  877. return error;
  878. /* Set up bmap header for xfs internal routine */
  879. bm.bmv_offset = BTOBB(start);
  880. /* Special case for whole file */
  881. if (length == FIEMAP_MAX_OFFSET)
  882. bm.bmv_length = -1LL;
  883. else
  884. bm.bmv_length = BTOBB(length);
  885. /* We add one because in getbmap world count includes the header */
  886. bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
  887. fieinfo->fi_extents_max + 1;
  888. bm.bmv_count = min_t(__s32, bm.bmv_count,
  889. (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
  890. bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
  891. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
  892. bm.bmv_iflags |= BMV_IF_ATTRFORK;
  893. if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
  894. bm.bmv_iflags |= BMV_IF_DELALLOC;
  895. error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
  896. if (error)
  897. return -error;
  898. return 0;
  899. }
  900. static const struct inode_operations xfs_inode_operations = {
  901. .get_acl = xfs_get_acl,
  902. .getattr = xfs_vn_getattr,
  903. .setattr = xfs_vn_setattr,
  904. .setxattr = generic_setxattr,
  905. .getxattr = generic_getxattr,
  906. .removexattr = generic_removexattr,
  907. .listxattr = xfs_vn_listxattr,
  908. .fiemap = xfs_vn_fiemap,
  909. };
  910. static const struct inode_operations xfs_dir_inode_operations = {
  911. .create = xfs_vn_create,
  912. .lookup = xfs_vn_lookup,
  913. .link = xfs_vn_link,
  914. .unlink = xfs_vn_unlink,
  915. .symlink = xfs_vn_symlink,
  916. .mkdir = xfs_vn_mkdir,
  917. /*
  918. * Yes, XFS uses the same method for rmdir and unlink.
  919. *
  920. * There are some subtile differences deeper in the code,
  921. * but we use S_ISDIR to check for those.
  922. */
  923. .rmdir = xfs_vn_unlink,
  924. .mknod = xfs_vn_mknod,
  925. .rename = xfs_vn_rename,
  926. .get_acl = xfs_get_acl,
  927. .getattr = xfs_vn_getattr,
  928. .setattr = xfs_vn_setattr,
  929. .setxattr = generic_setxattr,
  930. .getxattr = generic_getxattr,
  931. .removexattr = generic_removexattr,
  932. .listxattr = xfs_vn_listxattr,
  933. };
  934. static const struct inode_operations xfs_dir_ci_inode_operations = {
  935. .create = xfs_vn_create,
  936. .lookup = xfs_vn_ci_lookup,
  937. .link = xfs_vn_link,
  938. .unlink = xfs_vn_unlink,
  939. .symlink = xfs_vn_symlink,
  940. .mkdir = xfs_vn_mkdir,
  941. /*
  942. * Yes, XFS uses the same method for rmdir and unlink.
  943. *
  944. * There are some subtile differences deeper in the code,
  945. * but we use S_ISDIR to check for those.
  946. */
  947. .rmdir = xfs_vn_unlink,
  948. .mknod = xfs_vn_mknod,
  949. .rename = xfs_vn_rename,
  950. .get_acl = xfs_get_acl,
  951. .getattr = xfs_vn_getattr,
  952. .setattr = xfs_vn_setattr,
  953. .setxattr = generic_setxattr,
  954. .getxattr = generic_getxattr,
  955. .removexattr = generic_removexattr,
  956. .listxattr = xfs_vn_listxattr,
  957. };
  958. static const struct inode_operations xfs_symlink_inode_operations = {
  959. .readlink = generic_readlink,
  960. .follow_link = xfs_vn_follow_link,
  961. .put_link = xfs_vn_put_link,
  962. .get_acl = xfs_get_acl,
  963. .getattr = xfs_vn_getattr,
  964. .setattr = xfs_vn_setattr,
  965. .setxattr = generic_setxattr,
  966. .getxattr = generic_getxattr,
  967. .removexattr = generic_removexattr,
  968. .listxattr = xfs_vn_listxattr,
  969. };
  970. STATIC void
  971. xfs_diflags_to_iflags(
  972. struct inode *inode,
  973. struct xfs_inode *ip)
  974. {
  975. if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
  976. inode->i_flags |= S_IMMUTABLE;
  977. else
  978. inode->i_flags &= ~S_IMMUTABLE;
  979. if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
  980. inode->i_flags |= S_APPEND;
  981. else
  982. inode->i_flags &= ~S_APPEND;
  983. if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
  984. inode->i_flags |= S_SYNC;
  985. else
  986. inode->i_flags &= ~S_SYNC;
  987. if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
  988. inode->i_flags |= S_NOATIME;
  989. else
  990. inode->i_flags &= ~S_NOATIME;
  991. }
  992. /*
  993. * Initialize the Linux inode, set up the operation vectors and
  994. * unlock the inode.
  995. *
  996. * When reading existing inodes from disk this is called directly
  997. * from xfs_iget, when creating a new inode it is called from
  998. * xfs_ialloc after setting up the inode.
  999. *
  1000. * We are always called with an uninitialised linux inode here.
  1001. * We need to initialise the necessary fields and take a reference
  1002. * on it.
  1003. */
  1004. void
  1005. xfs_setup_inode(
  1006. struct xfs_inode *ip)
  1007. {
  1008. struct inode *inode = &ip->i_vnode;
  1009. inode->i_ino = ip->i_ino;
  1010. inode->i_state = I_NEW;
  1011. inode_sb_list_add(inode);
  1012. /* make the inode look hashed for the writeback code */
  1013. hlist_add_fake(&inode->i_hash);
  1014. inode->i_mode = ip->i_d.di_mode;
  1015. set_nlink(inode, ip->i_d.di_nlink);
  1016. inode->i_uid = ip->i_d.di_uid;
  1017. inode->i_gid = ip->i_d.di_gid;
  1018. switch (inode->i_mode & S_IFMT) {
  1019. case S_IFBLK:
  1020. case S_IFCHR:
  1021. inode->i_rdev =
  1022. MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  1023. sysv_minor(ip->i_df.if_u2.if_rdev));
  1024. break;
  1025. default:
  1026. inode->i_rdev = 0;
  1027. break;
  1028. }
  1029. inode->i_generation = ip->i_d.di_gen;
  1030. i_size_write(inode, ip->i_d.di_size);
  1031. inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
  1032. inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
  1033. inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  1034. inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  1035. inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  1036. inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  1037. xfs_diflags_to_iflags(inode, ip);
  1038. switch (inode->i_mode & S_IFMT) {
  1039. case S_IFREG:
  1040. inode->i_op = &xfs_inode_operations;
  1041. inode->i_fop = &xfs_file_operations;
  1042. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1043. break;
  1044. case S_IFDIR:
  1045. if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
  1046. inode->i_op = &xfs_dir_ci_inode_operations;
  1047. else
  1048. inode->i_op = &xfs_dir_inode_operations;
  1049. inode->i_fop = &xfs_dir_file_operations;
  1050. break;
  1051. case S_IFLNK:
  1052. inode->i_op = &xfs_symlink_inode_operations;
  1053. if (!(ip->i_df.if_flags & XFS_IFINLINE))
  1054. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1055. break;
  1056. default:
  1057. inode->i_op = &xfs_inode_operations;
  1058. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1059. break;
  1060. }
  1061. /*
  1062. * If there is no attribute fork no ACL can exist on this inode,
  1063. * and it can't have any file capabilities attached to it either.
  1064. */
  1065. if (!XFS_IFORK_Q(ip)) {
  1066. inode_has_no_xattr(inode);
  1067. cache_no_acl(inode);
  1068. }
  1069. xfs_iflags_clear(ip, XFS_INEW);
  1070. barrier();
  1071. unlock_new_inode(inode);
  1072. }