xfs_iops.c 33 KB

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