xfs_attr.c 38 KB

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