xfs_attr.c 37 KB

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