xfs_iops.c 34 KB

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