xfs_attr.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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_bit.h"
  25. #include "xfs_mount.h"
  26. #include "xfs_defer.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_attr_sf.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_alloc.h"
  32. #include "xfs_trans.h"
  33. #include "xfs_inode_item.h"
  34. #include "xfs_bmap.h"
  35. #include "xfs_bmap_util.h"
  36. #include "xfs_bmap_btree.h"
  37. #include "xfs_attr.h"
  38. #include "xfs_attr_leaf.h"
  39. #include "xfs_attr_remote.h"
  40. #include "xfs_error.h"
  41. #include "xfs_quota.h"
  42. #include "xfs_trans_space.h"
  43. #include "xfs_trace.h"
  44. /*
  45. * xfs_attr.c
  46. *
  47. * Provide the external interfaces to manage attribute lists.
  48. */
  49. /*========================================================================
  50. * Function prototypes for the kernel.
  51. *========================================================================*/
  52. /*
  53. * Internal routines when attribute list fits inside the inode.
  54. */
  55. STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
  56. /*
  57. * Internal routines when attribute list is one block.
  58. */
  59. STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
  60. STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
  61. STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
  62. /*
  63. * Internal routines when attribute list is more than one block.
  64. */
  65. STATIC int xfs_attr_node_get(xfs_da_args_t *args);
  66. STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
  67. STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
  68. STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
  69. STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
  70. STATIC int
  71. xfs_attr_args_init(
  72. struct xfs_da_args *args,
  73. struct xfs_inode *dp,
  74. const unsigned char *name,
  75. int flags)
  76. {
  77. if (!name)
  78. return -EINVAL;
  79. memset(args, 0, sizeof(*args));
  80. args->geo = dp->i_mount->m_attr_geo;
  81. args->whichfork = XFS_ATTR_FORK;
  82. args->dp = dp;
  83. args->flags = flags;
  84. args->name = name;
  85. args->namelen = strlen((const char *)name);
  86. if (args->namelen >= MAXNAMELEN)
  87. return -EFAULT; /* match IRIX behaviour */
  88. args->hashval = xfs_da_hashname(args->name, args->namelen);
  89. return 0;
  90. }
  91. int
  92. xfs_inode_hasattr(
  93. struct xfs_inode *ip)
  94. {
  95. if (!XFS_IFORK_Q(ip) ||
  96. (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  97. ip->i_d.di_anextents == 0))
  98. return 0;
  99. return 1;
  100. }
  101. /*========================================================================
  102. * Overall external interface routines.
  103. *========================================================================*/
  104. int
  105. xfs_attr_get(
  106. struct xfs_inode *ip,
  107. const unsigned char *name,
  108. unsigned char *value,
  109. int *valuelenp,
  110. int flags)
  111. {
  112. struct xfs_da_args args;
  113. uint lock_mode;
  114. int error;
  115. XFS_STATS_INC(ip->i_mount, xs_attr_get);
  116. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  117. return -EIO;
  118. error = xfs_attr_args_init(&args, ip, name, flags);
  119. if (error)
  120. return error;
  121. args.value = value;
  122. args.valuelen = *valuelenp;
  123. /* Entirely possible to look up a name which doesn't exist */
  124. args.op_flags = XFS_DA_OP_OKNOENT;
  125. lock_mode = xfs_ilock_attr_map_shared(ip);
  126. if (!xfs_inode_hasattr(ip))
  127. error = -ENOATTR;
  128. else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
  129. error = xfs_attr_shortform_getvalue(&args);
  130. else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
  131. error = xfs_attr_leaf_get(&args);
  132. else
  133. error = xfs_attr_node_get(&args);
  134. xfs_iunlock(ip, lock_mode);
  135. *valuelenp = args.valuelen;
  136. return error == -EEXIST ? 0 : error;
  137. }
  138. /*
  139. * Calculate how many blocks we need for the new attribute,
  140. */
  141. STATIC int
  142. xfs_attr_calc_size(
  143. struct xfs_da_args *args,
  144. int *local)
  145. {
  146. struct xfs_mount *mp = args->dp->i_mount;
  147. int size;
  148. int nblks;
  149. /*
  150. * Determine space new attribute will use, and if it would be
  151. * "local" or "remote" (note: local != inline).
  152. */
  153. size = xfs_attr_leaf_newentsize(args, local);
  154. nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
  155. if (*local) {
  156. if (size > (args->geo->blksize / 2)) {
  157. /* Double split possible */
  158. nblks *= 2;
  159. }
  160. } else {
  161. /*
  162. * Out of line attribute, cannot double split, but
  163. * make room for the attribute value itself.
  164. */
  165. uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
  166. nblks += dblocks;
  167. nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
  168. }
  169. return nblks;
  170. }
  171. int
  172. xfs_attr_set(
  173. struct xfs_inode *dp,
  174. const unsigned char *name,
  175. unsigned char *value,
  176. int valuelen,
  177. int flags)
  178. {
  179. struct xfs_mount *mp = dp->i_mount;
  180. struct xfs_da_args args;
  181. struct xfs_defer_ops dfops;
  182. struct xfs_trans_res tres;
  183. xfs_fsblock_t firstblock;
  184. int rsvd = (flags & ATTR_ROOT) != 0;
  185. int error, err2, local;
  186. XFS_STATS_INC(mp, xs_attr_set);
  187. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  188. return -EIO;
  189. error = xfs_attr_args_init(&args, dp, name, flags);
  190. if (error)
  191. return error;
  192. args.value = value;
  193. args.valuelen = valuelen;
  194. args.firstblock = &firstblock;
  195. args.dfops = &dfops;
  196. args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  197. args.total = xfs_attr_calc_size(&args, &local);
  198. error = xfs_qm_dqattach(dp, 0);
  199. if (error)
  200. return error;
  201. /*
  202. * If the inode doesn't have an attribute fork, add one.
  203. * (inode must not be locked when we call this routine)
  204. */
  205. if (XFS_IFORK_Q(dp) == 0) {
  206. int sf_size = sizeof(xfs_attr_sf_hdr_t) +
  207. XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
  208. error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
  209. if (error)
  210. return error;
  211. }
  212. tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
  213. M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
  214. tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
  215. tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  216. /*
  217. * Root fork attributes can use reserved data blocks for this
  218. * operation if necessary
  219. */
  220. error = xfs_trans_alloc(mp, &tres, args.total, 0,
  221. rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
  222. if (error)
  223. return error;
  224. xfs_ilock(dp, XFS_ILOCK_EXCL);
  225. error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
  226. rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  227. XFS_QMOPT_RES_REGBLKS);
  228. if (error) {
  229. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  230. xfs_trans_cancel(args.trans);
  231. return error;
  232. }
  233. xfs_trans_ijoin(args.trans, dp, 0);
  234. /*
  235. * If the attribute list is non-existent or a shortform list,
  236. * upgrade it to a single-leaf-block attribute list.
  237. */
  238. if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
  239. (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  240. dp->i_d.di_anextents == 0)) {
  241. /*
  242. * Build initial attribute list (if required).
  243. */
  244. if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
  245. xfs_attr_shortform_create(&args);
  246. /*
  247. * Try to add the attr to the attribute list in
  248. * the inode.
  249. */
  250. error = xfs_attr_shortform_addname(&args);
  251. if (error != -ENOSPC) {
  252. /*
  253. * Commit the shortform mods, and we're done.
  254. * NOTE: this is also the error path (EEXIST, etc).
  255. */
  256. ASSERT(args.trans != NULL);
  257. /*
  258. * If this is a synchronous mount, make sure that
  259. * the transaction goes to disk before returning
  260. * to the user.
  261. */
  262. if (mp->m_flags & XFS_MOUNT_WSYNC)
  263. xfs_trans_set_sync(args.trans);
  264. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  265. xfs_trans_ichgtime(args.trans, dp,
  266. XFS_ICHGTIME_CHG);
  267. }
  268. err2 = xfs_trans_commit(args.trans);
  269. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  270. return error ? error : err2;
  271. }
  272. /*
  273. * It won't fit in the shortform, transform to a leaf block.
  274. * GROT: another possible req'mt for a double-split btree op.
  275. */
  276. xfs_defer_init(args.dfops, args.firstblock);
  277. error = xfs_attr_shortform_to_leaf(&args);
  278. if (!error)
  279. error = xfs_defer_finish(&args.trans, args.dfops, dp);
  280. if (error) {
  281. args.trans = NULL;
  282. xfs_defer_cancel(&dfops);
  283. goto out;
  284. }
  285. /*
  286. * Commit the leaf transformation. We'll need another (linked)
  287. * transaction to add the new attribute to the leaf.
  288. */
  289. error = xfs_trans_roll(&args.trans, dp);
  290. if (error)
  291. goto out;
  292. }
  293. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
  294. error = xfs_attr_leaf_addname(&args);
  295. else
  296. error = xfs_attr_node_addname(&args);
  297. if (error)
  298. goto out;
  299. /*
  300. * If this is a synchronous mount, make sure that the
  301. * transaction goes to disk before returning to the user.
  302. */
  303. if (mp->m_flags & XFS_MOUNT_WSYNC)
  304. xfs_trans_set_sync(args.trans);
  305. if ((flags & ATTR_KERNOTIME) == 0)
  306. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  307. /*
  308. * Commit the last in the sequence of transactions.
  309. */
  310. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  311. error = xfs_trans_commit(args.trans);
  312. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  313. return error;
  314. out:
  315. if (args.trans)
  316. xfs_trans_cancel(args.trans);
  317. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  318. return error;
  319. }
  320. /*
  321. * Generic handler routine to remove a name from an attribute list.
  322. * Transitions attribute list from Btree to shortform as necessary.
  323. */
  324. int
  325. xfs_attr_remove(
  326. struct xfs_inode *dp,
  327. const unsigned char *name,
  328. int flags)
  329. {
  330. struct xfs_mount *mp = dp->i_mount;
  331. struct xfs_da_args args;
  332. struct xfs_defer_ops dfops;
  333. xfs_fsblock_t firstblock;
  334. int error;
  335. XFS_STATS_INC(mp, xs_attr_remove);
  336. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  337. return -EIO;
  338. error = xfs_attr_args_init(&args, dp, name, flags);
  339. if (error)
  340. return error;
  341. args.firstblock = &firstblock;
  342. args.dfops = &dfops;
  343. /*
  344. * we have no control over the attribute names that userspace passes us
  345. * to remove, so we have to allow the name lookup prior to attribute
  346. * removal to fail.
  347. */
  348. args.op_flags = XFS_DA_OP_OKNOENT;
  349. error = xfs_qm_dqattach(dp, 0);
  350. if (error)
  351. return error;
  352. /*
  353. * Root fork attributes can use reserved data blocks for this
  354. * operation if necessary
  355. */
  356. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
  357. XFS_ATTRRM_SPACE_RES(mp), 0,
  358. (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
  359. &args.trans);
  360. if (error)
  361. return error;
  362. xfs_ilock(dp, XFS_ILOCK_EXCL);
  363. /*
  364. * No need to make quota reservations here. We expect to release some
  365. * blocks not allocate in the common case.
  366. */
  367. xfs_trans_ijoin(args.trans, dp, 0);
  368. if (!xfs_inode_hasattr(dp)) {
  369. error = -ENOATTR;
  370. } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  371. ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
  372. error = xfs_attr_shortform_remove(&args);
  373. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  374. error = xfs_attr_leaf_removename(&args);
  375. } else {
  376. error = xfs_attr_node_removename(&args);
  377. }
  378. if (error)
  379. goto out;
  380. /*
  381. * If this is a synchronous mount, make sure that the
  382. * transaction goes to disk before returning to the user.
  383. */
  384. if (mp->m_flags & XFS_MOUNT_WSYNC)
  385. xfs_trans_set_sync(args.trans);
  386. if ((flags & ATTR_KERNOTIME) == 0)
  387. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  388. /*
  389. * Commit the last in the sequence of transactions.
  390. */
  391. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  392. error = xfs_trans_commit(args.trans);
  393. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  394. return error;
  395. out:
  396. if (args.trans)
  397. xfs_trans_cancel(args.trans);
  398. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  399. return error;
  400. }
  401. /*========================================================================
  402. * External routines when attribute list is inside the inode
  403. *========================================================================*/
  404. /*
  405. * Add a name to the shortform attribute list structure
  406. * This is the external routine.
  407. */
  408. STATIC int
  409. xfs_attr_shortform_addname(xfs_da_args_t *args)
  410. {
  411. int newsize, forkoff, retval;
  412. trace_xfs_attr_sf_addname(args);
  413. retval = xfs_attr_shortform_lookup(args);
  414. if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
  415. return retval;
  416. } else if (retval == -EEXIST) {
  417. if (args->flags & ATTR_CREATE)
  418. return retval;
  419. retval = xfs_attr_shortform_remove(args);
  420. ASSERT(retval == 0);
  421. }
  422. if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
  423. args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  424. return -ENOSPC;
  425. newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
  426. newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
  427. forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
  428. if (!forkoff)
  429. return -ENOSPC;
  430. xfs_attr_shortform_add(args, forkoff);
  431. return 0;
  432. }
  433. /*========================================================================
  434. * External routines when attribute list is one block
  435. *========================================================================*/
  436. /*
  437. * Add a name to the leaf attribute list structure
  438. *
  439. * This leaf block cannot have a "remote" value, we only call this routine
  440. * if bmap_one_block() says there is only one block (ie: no remote blks).
  441. */
  442. STATIC int
  443. xfs_attr_leaf_addname(xfs_da_args_t *args)
  444. {
  445. xfs_inode_t *dp;
  446. struct xfs_buf *bp;
  447. int retval, error, forkoff;
  448. trace_xfs_attr_leaf_addname(args);
  449. /*
  450. * Read the (only) block in the attribute list in.
  451. */
  452. dp = args->dp;
  453. args->blkno = 0;
  454. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  455. if (error)
  456. return error;
  457. /*
  458. * Look up the given attribute in the leaf block. Figure out if
  459. * the given flags produce an error or call for an atomic rename.
  460. */
  461. retval = xfs_attr3_leaf_lookup_int(bp, args);
  462. if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
  463. xfs_trans_brelse(args->trans, bp);
  464. return retval;
  465. } else if (retval == -EEXIST) {
  466. if (args->flags & ATTR_CREATE) { /* pure create op */
  467. xfs_trans_brelse(args->trans, bp);
  468. return retval;
  469. }
  470. trace_xfs_attr_leaf_replace(args);
  471. /* save the attribute state for later removal*/
  472. args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
  473. args->blkno2 = args->blkno; /* set 2nd entry info*/
  474. args->index2 = args->index;
  475. args->rmtblkno2 = args->rmtblkno;
  476. args->rmtblkcnt2 = args->rmtblkcnt;
  477. args->rmtvaluelen2 = args->rmtvaluelen;
  478. /*
  479. * clear the remote attr state now that it is saved so that the
  480. * values reflect the state of the attribute we are about to
  481. * add, not the attribute we just found and will remove later.
  482. */
  483. args->rmtblkno = 0;
  484. args->rmtblkcnt = 0;
  485. args->rmtvaluelen = 0;
  486. }
  487. /*
  488. * Add the attribute to the leaf block, transitioning to a Btree
  489. * if required.
  490. */
  491. retval = xfs_attr3_leaf_add(bp, args);
  492. if (retval == -ENOSPC) {
  493. /*
  494. * Promote the attribute list to the Btree format, then
  495. * Commit that transaction so that the node_addname() call
  496. * can manage its own transactions.
  497. */
  498. xfs_defer_init(args->dfops, args->firstblock);
  499. error = xfs_attr3_leaf_to_node(args);
  500. if (!error)
  501. error = xfs_defer_finish(&args->trans, args->dfops, dp);
  502. if (error) {
  503. args->trans = NULL;
  504. xfs_defer_cancel(args->dfops);
  505. return error;
  506. }
  507. /*
  508. * Commit the current trans (including the inode) and start
  509. * a new one.
  510. */
  511. error = xfs_trans_roll(&args->trans, dp);
  512. if (error)
  513. return error;
  514. /*
  515. * Fob the whole rest of the problem off on the Btree code.
  516. */
  517. error = xfs_attr_node_addname(args);
  518. return error;
  519. }
  520. /*
  521. * Commit the transaction that added the attr name so that
  522. * later routines can manage their own transactions.
  523. */
  524. error = xfs_trans_roll(&args->trans, dp);
  525. if (error)
  526. return error;
  527. /*
  528. * If there was an out-of-line value, allocate the blocks we
  529. * identified for its storage and copy the value. This is done
  530. * after we create the attribute so that we don't overflow the
  531. * maximum size of a transaction and/or hit a deadlock.
  532. */
  533. if (args->rmtblkno > 0) {
  534. error = xfs_attr_rmtval_set(args);
  535. if (error)
  536. return error;
  537. }
  538. /*
  539. * If this is an atomic rename operation, we must "flip" the
  540. * incomplete flags on the "new" and "old" attribute/value pairs
  541. * so that one disappears and one appears atomically. Then we
  542. * must remove the "old" attribute/value pair.
  543. */
  544. if (args->op_flags & XFS_DA_OP_RENAME) {
  545. /*
  546. * In a separate transaction, set the incomplete flag on the
  547. * "old" attr and clear the incomplete flag on the "new" attr.
  548. */
  549. error = xfs_attr3_leaf_flipflags(args);
  550. if (error)
  551. return error;
  552. /*
  553. * Dismantle the "old" attribute/value pair by removing
  554. * a "remote" value (if it exists).
  555. */
  556. args->index = args->index2;
  557. args->blkno = args->blkno2;
  558. args->rmtblkno = args->rmtblkno2;
  559. args->rmtblkcnt = args->rmtblkcnt2;
  560. args->rmtvaluelen = args->rmtvaluelen2;
  561. if (args->rmtblkno) {
  562. error = xfs_attr_rmtval_remove(args);
  563. if (error)
  564. return error;
  565. }
  566. /*
  567. * Read in the block containing the "old" attr, then
  568. * remove the "old" attr from that block (neat, huh!)
  569. */
  570. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
  571. -1, &bp);
  572. if (error)
  573. return error;
  574. xfs_attr3_leaf_remove(bp, args);
  575. /*
  576. * If the result is small enough, shrink it all into the inode.
  577. */
  578. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  579. xfs_defer_init(args->dfops, args->firstblock);
  580. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  581. /* bp is gone due to xfs_da_shrink_inode */
  582. if (!error)
  583. error = xfs_defer_finish(&args->trans,
  584. args->dfops, dp);
  585. if (error) {
  586. args->trans = NULL;
  587. xfs_defer_cancel(args->dfops);
  588. return error;
  589. }
  590. }
  591. /*
  592. * Commit the remove and start the next trans in series.
  593. */
  594. error = xfs_trans_roll(&args->trans, dp);
  595. } else if (args->rmtblkno > 0) {
  596. /*
  597. * Added a "remote" value, just clear the incomplete flag.
  598. */
  599. error = xfs_attr3_leaf_clearflag(args);
  600. }
  601. return error;
  602. }
  603. /*
  604. * Remove a name from the leaf attribute list structure
  605. *
  606. * This leaf block cannot have a "remote" value, we only call this routine
  607. * if bmap_one_block() says there is only one block (ie: no remote blks).
  608. */
  609. STATIC int
  610. xfs_attr_leaf_removename(xfs_da_args_t *args)
  611. {
  612. xfs_inode_t *dp;
  613. struct xfs_buf *bp;
  614. int error, forkoff;
  615. trace_xfs_attr_leaf_removename(args);
  616. /*
  617. * Remove the attribute.
  618. */
  619. dp = args->dp;
  620. args->blkno = 0;
  621. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  622. if (error)
  623. return error;
  624. error = xfs_attr3_leaf_lookup_int(bp, args);
  625. if (error == -ENOATTR) {
  626. xfs_trans_brelse(args->trans, bp);
  627. return error;
  628. }
  629. xfs_attr3_leaf_remove(bp, args);
  630. /*
  631. * If the result is small enough, shrink it all into the inode.
  632. */
  633. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  634. xfs_defer_init(args->dfops, args->firstblock);
  635. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  636. /* bp is gone due to xfs_da_shrink_inode */
  637. if (!error)
  638. error = xfs_defer_finish(&args->trans, args->dfops, dp);
  639. if (error) {
  640. args->trans = NULL;
  641. xfs_defer_cancel(args->dfops);
  642. return error;
  643. }
  644. }
  645. return 0;
  646. }
  647. /*
  648. * Look up a name in a leaf attribute list structure.
  649. *
  650. * This leaf block cannot have a "remote" value, we only call this routine
  651. * if bmap_one_block() says there is only one block (ie: no remote blks).
  652. */
  653. STATIC int
  654. xfs_attr_leaf_get(xfs_da_args_t *args)
  655. {
  656. struct xfs_buf *bp;
  657. int error;
  658. trace_xfs_attr_leaf_get(args);
  659. args->blkno = 0;
  660. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  661. if (error)
  662. return error;
  663. error = xfs_attr3_leaf_lookup_int(bp, args);
  664. if (error != -EEXIST) {
  665. xfs_trans_brelse(args->trans, bp);
  666. return error;
  667. }
  668. error = xfs_attr3_leaf_getvalue(bp, args);
  669. xfs_trans_brelse(args->trans, bp);
  670. if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
  671. error = xfs_attr_rmtval_get(args);
  672. }
  673. return error;
  674. }
  675. /*========================================================================
  676. * External routines when attribute list size > geo->blksize
  677. *========================================================================*/
  678. /*
  679. * Add a name to a Btree-format attribute list.
  680. *
  681. * This will involve walking down the Btree, and may involve splitting
  682. * leaf nodes and even splitting intermediate nodes up to and including
  683. * the root node (a special case of an intermediate node).
  684. *
  685. * "Remote" attribute values confuse the issue and atomic rename operations
  686. * add a whole extra layer of confusion on top of that.
  687. */
  688. STATIC int
  689. xfs_attr_node_addname(xfs_da_args_t *args)
  690. {
  691. xfs_da_state_t *state;
  692. xfs_da_state_blk_t *blk;
  693. xfs_inode_t *dp;
  694. xfs_mount_t *mp;
  695. int retval, error;
  696. trace_xfs_attr_node_addname(args);
  697. /*
  698. * Fill in bucket of arguments/results/context to carry around.
  699. */
  700. dp = args->dp;
  701. mp = dp->i_mount;
  702. restart:
  703. state = xfs_da_state_alloc();
  704. state->args = args;
  705. state->mp = mp;
  706. /*
  707. * Search to see if name already exists, and get back a pointer
  708. * to where it should go.
  709. */
  710. error = xfs_da3_node_lookup_int(state, &retval);
  711. if (error)
  712. goto out;
  713. blk = &state->path.blk[ state->path.active-1 ];
  714. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  715. if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
  716. goto out;
  717. } else if (retval == -EEXIST) {
  718. if (args->flags & ATTR_CREATE)
  719. goto out;
  720. trace_xfs_attr_node_replace(args);
  721. /* save the attribute state for later removal*/
  722. args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
  723. args->blkno2 = args->blkno; /* set 2nd entry info*/
  724. args->index2 = args->index;
  725. args->rmtblkno2 = args->rmtblkno;
  726. args->rmtblkcnt2 = args->rmtblkcnt;
  727. args->rmtvaluelen2 = args->rmtvaluelen;
  728. /*
  729. * clear the remote attr state now that it is saved so that the
  730. * values reflect the state of the attribute we are about to
  731. * add, not the attribute we just found and will remove later.
  732. */
  733. args->rmtblkno = 0;
  734. args->rmtblkcnt = 0;
  735. args->rmtvaluelen = 0;
  736. }
  737. retval = xfs_attr3_leaf_add(blk->bp, state->args);
  738. if (retval == -ENOSPC) {
  739. if (state->path.active == 1) {
  740. /*
  741. * Its really a single leaf node, but it had
  742. * out-of-line values so it looked like it *might*
  743. * have been a b-tree.
  744. */
  745. xfs_da_state_free(state);
  746. state = NULL;
  747. xfs_defer_init(args->dfops, args->firstblock);
  748. error = xfs_attr3_leaf_to_node(args);
  749. if (!error)
  750. error = xfs_defer_finish(&args->trans,
  751. args->dfops, dp);
  752. if (error) {
  753. args->trans = NULL;
  754. xfs_defer_cancel(args->dfops);
  755. goto out;
  756. }
  757. /*
  758. * Commit the node conversion and start the next
  759. * trans in the chain.
  760. */
  761. error = xfs_trans_roll(&args->trans, dp);
  762. if (error)
  763. goto out;
  764. goto restart;
  765. }
  766. /*
  767. * Split as many Btree elements as required.
  768. * This code tracks the new and old attr's location
  769. * in the index/blkno/rmtblkno/rmtblkcnt fields and
  770. * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
  771. */
  772. xfs_defer_init(args->dfops, args->firstblock);
  773. error = xfs_da3_split(state);
  774. if (!error)
  775. error = xfs_defer_finish(&args->trans, args->dfops, dp);
  776. if (error) {
  777. args->trans = NULL;
  778. xfs_defer_cancel(args->dfops);
  779. goto out;
  780. }
  781. } else {
  782. /*
  783. * Addition succeeded, update Btree hashvals.
  784. */
  785. xfs_da3_fixhashpath(state, &state->path);
  786. }
  787. /*
  788. * Kill the state structure, we're done with it and need to
  789. * allow the buffers to come back later.
  790. */
  791. xfs_da_state_free(state);
  792. state = NULL;
  793. /*
  794. * Commit the leaf addition or btree split and start the next
  795. * trans in the chain.
  796. */
  797. error = xfs_trans_roll(&args->trans, dp);
  798. if (error)
  799. goto out;
  800. /*
  801. * If there was an out-of-line value, allocate the blocks we
  802. * identified for its storage and copy the value. This is done
  803. * after we create the attribute so that we don't overflow the
  804. * maximum size of a transaction and/or hit a deadlock.
  805. */
  806. if (args->rmtblkno > 0) {
  807. error = xfs_attr_rmtval_set(args);
  808. if (error)
  809. return error;
  810. }
  811. /*
  812. * If this is an atomic rename operation, we must "flip" the
  813. * incomplete flags on the "new" and "old" attribute/value pairs
  814. * so that one disappears and one appears atomically. Then we
  815. * must remove the "old" attribute/value pair.
  816. */
  817. if (args->op_flags & XFS_DA_OP_RENAME) {
  818. /*
  819. * In a separate transaction, set the incomplete flag on the
  820. * "old" attr and clear the incomplete flag on the "new" attr.
  821. */
  822. error = xfs_attr3_leaf_flipflags(args);
  823. if (error)
  824. goto out;
  825. /*
  826. * Dismantle the "old" attribute/value pair by removing
  827. * a "remote" value (if it exists).
  828. */
  829. args->index = args->index2;
  830. args->blkno = args->blkno2;
  831. args->rmtblkno = args->rmtblkno2;
  832. args->rmtblkcnt = args->rmtblkcnt2;
  833. args->rmtvaluelen = args->rmtvaluelen2;
  834. if (args->rmtblkno) {
  835. error = xfs_attr_rmtval_remove(args);
  836. if (error)
  837. return error;
  838. }
  839. /*
  840. * Re-find the "old" attribute entry after any split ops.
  841. * The INCOMPLETE flag means that we will find the "old"
  842. * attr, not the "new" one.
  843. */
  844. args->flags |= XFS_ATTR_INCOMPLETE;
  845. state = xfs_da_state_alloc();
  846. state->args = args;
  847. state->mp = mp;
  848. state->inleaf = 0;
  849. error = xfs_da3_node_lookup_int(state, &retval);
  850. if (error)
  851. goto out;
  852. /*
  853. * Remove the name and update the hashvals in the tree.
  854. */
  855. blk = &state->path.blk[ state->path.active-1 ];
  856. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  857. error = xfs_attr3_leaf_remove(blk->bp, args);
  858. xfs_da3_fixhashpath(state, &state->path);
  859. /*
  860. * Check to see if the tree needs to be collapsed.
  861. */
  862. if (retval && (state->path.active > 1)) {
  863. xfs_defer_init(args->dfops, args->firstblock);
  864. error = xfs_da3_join(state);
  865. if (!error)
  866. error = xfs_defer_finish(&args->trans,
  867. args->dfops, dp);
  868. if (error) {
  869. args->trans = NULL;
  870. xfs_defer_cancel(args->dfops);
  871. goto out;
  872. }
  873. }
  874. /*
  875. * Commit and start the next trans in the chain.
  876. */
  877. error = xfs_trans_roll(&args->trans, dp);
  878. if (error)
  879. goto out;
  880. } else if (args->rmtblkno > 0) {
  881. /*
  882. * Added a "remote" value, just clear the incomplete flag.
  883. */
  884. error = xfs_attr3_leaf_clearflag(args);
  885. if (error)
  886. goto out;
  887. }
  888. retval = error = 0;
  889. out:
  890. if (state)
  891. xfs_da_state_free(state);
  892. if (error)
  893. return error;
  894. return retval;
  895. }
  896. /*
  897. * Remove a name from a B-tree attribute list.
  898. *
  899. * This will involve walking down the Btree, and may involve joining
  900. * leaf nodes and even joining intermediate nodes up to and including
  901. * the root node (a special case of an intermediate node).
  902. */
  903. STATIC int
  904. xfs_attr_node_removename(xfs_da_args_t *args)
  905. {
  906. xfs_da_state_t *state;
  907. xfs_da_state_blk_t *blk;
  908. xfs_inode_t *dp;
  909. struct xfs_buf *bp;
  910. int retval, error, forkoff;
  911. trace_xfs_attr_node_removename(args);
  912. /*
  913. * Tie a string around our finger to remind us where we are.
  914. */
  915. dp = args->dp;
  916. state = xfs_da_state_alloc();
  917. state->args = args;
  918. state->mp = dp->i_mount;
  919. /*
  920. * Search to see if name exists, and get back a pointer to it.
  921. */
  922. error = xfs_da3_node_lookup_int(state, &retval);
  923. if (error || (retval != -EEXIST)) {
  924. if (error == 0)
  925. error = retval;
  926. goto out;
  927. }
  928. /*
  929. * If there is an out-of-line value, de-allocate the blocks.
  930. * This is done before we remove the attribute so that we don't
  931. * overflow the maximum size of a transaction and/or hit a deadlock.
  932. */
  933. blk = &state->path.blk[ state->path.active-1 ];
  934. ASSERT(blk->bp != NULL);
  935. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  936. if (args->rmtblkno > 0) {
  937. /*
  938. * Fill in disk block numbers in the state structure
  939. * so that we can get the buffers back after we commit
  940. * several transactions in the following calls.
  941. */
  942. error = xfs_attr_fillstate(state);
  943. if (error)
  944. goto out;
  945. /*
  946. * Mark the attribute as INCOMPLETE, then bunmapi() the
  947. * remote value.
  948. */
  949. error = xfs_attr3_leaf_setflag(args);
  950. if (error)
  951. goto out;
  952. error = xfs_attr_rmtval_remove(args);
  953. if (error)
  954. goto out;
  955. /*
  956. * Refill the state structure with buffers, the prior calls
  957. * released our buffers.
  958. */
  959. error = xfs_attr_refillstate(state);
  960. if (error)
  961. goto out;
  962. }
  963. /*
  964. * Remove the name and update the hashvals in the tree.
  965. */
  966. blk = &state->path.blk[ state->path.active-1 ];
  967. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  968. retval = xfs_attr3_leaf_remove(blk->bp, args);
  969. xfs_da3_fixhashpath(state, &state->path);
  970. /*
  971. * Check to see if the tree needs to be collapsed.
  972. */
  973. if (retval && (state->path.active > 1)) {
  974. xfs_defer_init(args->dfops, args->firstblock);
  975. error = xfs_da3_join(state);
  976. if (!error)
  977. error = xfs_defer_finish(&args->trans, args->dfops, dp);
  978. if (error) {
  979. args->trans = NULL;
  980. xfs_defer_cancel(args->dfops);
  981. goto out;
  982. }
  983. /*
  984. * Commit the Btree join operation and start a new trans.
  985. */
  986. error = xfs_trans_roll(&args->trans, dp);
  987. if (error)
  988. goto out;
  989. }
  990. /*
  991. * If the result is small enough, push it all into the inode.
  992. */
  993. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  994. /*
  995. * Have to get rid of the copy of this dabuf in the state.
  996. */
  997. ASSERT(state->path.active == 1);
  998. ASSERT(state->path.blk[0].bp);
  999. state->path.blk[0].bp = NULL;
  1000. error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
  1001. if (error)
  1002. goto out;
  1003. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  1004. xfs_defer_init(args->dfops, args->firstblock);
  1005. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  1006. /* bp is gone due to xfs_da_shrink_inode */
  1007. if (!error)
  1008. error = xfs_defer_finish(&args->trans,
  1009. args->dfops, dp);
  1010. if (error) {
  1011. args->trans = NULL;
  1012. xfs_defer_cancel(args->dfops);
  1013. goto out;
  1014. }
  1015. } else
  1016. xfs_trans_brelse(args->trans, bp);
  1017. }
  1018. error = 0;
  1019. out:
  1020. xfs_da_state_free(state);
  1021. return error;
  1022. }
  1023. /*
  1024. * Fill in the disk block numbers in the state structure for the buffers
  1025. * that are attached to the state structure.
  1026. * This is done so that we can quickly reattach ourselves to those buffers
  1027. * after some set of transaction commits have released these buffers.
  1028. */
  1029. STATIC int
  1030. xfs_attr_fillstate(xfs_da_state_t *state)
  1031. {
  1032. xfs_da_state_path_t *path;
  1033. xfs_da_state_blk_t *blk;
  1034. int level;
  1035. trace_xfs_attr_fillstate(state->args);
  1036. /*
  1037. * Roll down the "path" in the state structure, storing the on-disk
  1038. * block number for those buffers in the "path".
  1039. */
  1040. path = &state->path;
  1041. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1042. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1043. if (blk->bp) {
  1044. blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
  1045. blk->bp = NULL;
  1046. } else {
  1047. blk->disk_blkno = 0;
  1048. }
  1049. }
  1050. /*
  1051. * Roll down the "altpath" in the state structure, storing the on-disk
  1052. * block number for those buffers in the "altpath".
  1053. */
  1054. path = &state->altpath;
  1055. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1056. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1057. if (blk->bp) {
  1058. blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
  1059. blk->bp = NULL;
  1060. } else {
  1061. blk->disk_blkno = 0;
  1062. }
  1063. }
  1064. return 0;
  1065. }
  1066. /*
  1067. * Reattach the buffers to the state structure based on the disk block
  1068. * numbers stored in the state structure.
  1069. * This is done after some set of transaction commits have released those
  1070. * buffers from our grip.
  1071. */
  1072. STATIC int
  1073. xfs_attr_refillstate(xfs_da_state_t *state)
  1074. {
  1075. xfs_da_state_path_t *path;
  1076. xfs_da_state_blk_t *blk;
  1077. int level, error;
  1078. trace_xfs_attr_refillstate(state->args);
  1079. /*
  1080. * Roll down the "path" in the state structure, storing the on-disk
  1081. * block number for those buffers in the "path".
  1082. */
  1083. path = &state->path;
  1084. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1085. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1086. if (blk->disk_blkno) {
  1087. error = xfs_da3_node_read(state->args->trans,
  1088. state->args->dp,
  1089. blk->blkno, blk->disk_blkno,
  1090. &blk->bp, XFS_ATTR_FORK);
  1091. if (error)
  1092. return error;
  1093. } else {
  1094. blk->bp = NULL;
  1095. }
  1096. }
  1097. /*
  1098. * Roll down the "altpath" in the state structure, storing the on-disk
  1099. * block number for those buffers in the "altpath".
  1100. */
  1101. path = &state->altpath;
  1102. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1103. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1104. if (blk->disk_blkno) {
  1105. error = xfs_da3_node_read(state->args->trans,
  1106. state->args->dp,
  1107. blk->blkno, blk->disk_blkno,
  1108. &blk->bp, XFS_ATTR_FORK);
  1109. if (error)
  1110. return error;
  1111. } else {
  1112. blk->bp = NULL;
  1113. }
  1114. }
  1115. return 0;
  1116. }
  1117. /*
  1118. * Look up a filename in a node attribute list.
  1119. *
  1120. * This routine gets called for any attribute fork that has more than one
  1121. * block, ie: both true Btree attr lists and for single-leaf-blocks with
  1122. * "remote" values taking up more blocks.
  1123. */
  1124. STATIC int
  1125. xfs_attr_node_get(xfs_da_args_t *args)
  1126. {
  1127. xfs_da_state_t *state;
  1128. xfs_da_state_blk_t *blk;
  1129. int error, retval;
  1130. int i;
  1131. trace_xfs_attr_node_get(args);
  1132. state = xfs_da_state_alloc();
  1133. state->args = args;
  1134. state->mp = args->dp->i_mount;
  1135. /*
  1136. * Search to see if name exists, and get back a pointer to it.
  1137. */
  1138. error = xfs_da3_node_lookup_int(state, &retval);
  1139. if (error) {
  1140. retval = error;
  1141. } else if (retval == -EEXIST) {
  1142. blk = &state->path.blk[ state->path.active-1 ];
  1143. ASSERT(blk->bp != NULL);
  1144. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1145. /*
  1146. * Get the value, local or "remote"
  1147. */
  1148. retval = xfs_attr3_leaf_getvalue(blk->bp, args);
  1149. if (!retval && (args->rmtblkno > 0)
  1150. && !(args->flags & ATTR_KERNOVAL)) {
  1151. retval = xfs_attr_rmtval_get(args);
  1152. }
  1153. }
  1154. /*
  1155. * If not in a transaction, we have to release all the buffers.
  1156. */
  1157. for (i = 0; i < state->path.active; i++) {
  1158. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  1159. state->path.blk[i].bp = NULL;
  1160. }
  1161. xfs_da_state_free(state);
  1162. return retval;
  1163. }