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_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_trans_priv.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_attr_sf.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_alloc.h"
  34. #include "xfs_inode_item.h"
  35. #include "xfs_bmap.h"
  36. #include "xfs_bmap_util.h"
  37. #include "xfs_attr.h"
  38. #include "xfs_attr_leaf.h"
  39. #include "xfs_attr_remote.h"
  40. #include "xfs_error.h"
  41. #include "xfs_quota.h"
  42. #include "xfs_trans_space.h"
  43. #include "xfs_vnodeops.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. xfs_mount_t *mp = dp->i_mount;
  209. int rsvd = (flags & ATTR_ROOT) != 0;
  210. int local;
  211. /*
  212. * Attach the dquots to the inode.
  213. */
  214. error = xfs_qm_dqattach(dp, 0);
  215. if (error)
  216. return error;
  217. /*
  218. * If the inode doesn't have an attribute fork, add one.
  219. * (inode must not be locked when we call this routine)
  220. */
  221. if (XFS_IFORK_Q(dp) == 0) {
  222. int sf_size = sizeof(xfs_attr_sf_hdr_t) +
  223. XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
  224. if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
  225. return(error);
  226. }
  227. /*
  228. * Fill in the arg structure for this request.
  229. */
  230. memset((char *)&args, 0, sizeof(args));
  231. args.name = name->name;
  232. args.namelen = name->len;
  233. args.value = value;
  234. args.valuelen = valuelen;
  235. args.flags = flags;
  236. args.hashval = xfs_da_hashname(args.name, args.namelen);
  237. args.dp = dp;
  238. args.firstblock = &firstblock;
  239. args.flist = &flist;
  240. args.whichfork = XFS_ATTR_FORK;
  241. args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  242. /* Size is now blocks for attribute data */
  243. args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
  244. /*
  245. * Start our first transaction of the day.
  246. *
  247. * All future transactions during this code must be "chained" off
  248. * this one via the trans_dup() call. All transactions will contain
  249. * the inode, and the inode will always be marked with trans_ihold().
  250. * Since the inode will be locked in all transactions, we must log
  251. * the inode in every transaction to let it float upward through
  252. * the log.
  253. */
  254. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
  255. /*
  256. * Root fork attributes can use reserved data blocks for this
  257. * operation if necessary
  258. */
  259. if (rsvd)
  260. args.trans->t_flags |= XFS_TRANS_RESERVE;
  261. error = xfs_trans_reserve(args.trans, args.total,
  262. XFS_ATTRSETM_LOG_RES(mp) +
  263. XFS_ATTRSETRT_LOG_RES(mp) * args.total,
  264. 0, XFS_TRANS_PERM_LOG_RES,
  265. XFS_ATTRSET_LOG_COUNT);
  266. if (error) {
  267. xfs_trans_cancel(args.trans, 0);
  268. return(error);
  269. }
  270. xfs_ilock(dp, XFS_ILOCK_EXCL);
  271. error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
  272. rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  273. XFS_QMOPT_RES_REGBLKS);
  274. if (error) {
  275. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  276. xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  277. return (error);
  278. }
  279. xfs_trans_ijoin(args.trans, dp, 0);
  280. /*
  281. * If the attribute list is non-existent or a shortform list,
  282. * upgrade it to a single-leaf-block attribute list.
  283. */
  284. if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
  285. ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
  286. (dp->i_d.di_anextents == 0))) {
  287. /*
  288. * Build initial attribute list (if required).
  289. */
  290. if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
  291. xfs_attr_shortform_create(&args);
  292. /*
  293. * Try to add the attr to the attribute list in
  294. * the inode.
  295. */
  296. error = xfs_attr_shortform_addname(&args);
  297. if (error != ENOSPC) {
  298. /*
  299. * Commit the shortform mods, and we're done.
  300. * NOTE: this is also the error path (EEXIST, etc).
  301. */
  302. ASSERT(args.trans != NULL);
  303. /*
  304. * If this is a synchronous mount, make sure that
  305. * the transaction goes to disk before returning
  306. * to the user.
  307. */
  308. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  309. xfs_trans_set_sync(args.trans);
  310. }
  311. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  312. xfs_trans_ichgtime(args.trans, dp,
  313. XFS_ICHGTIME_CHG);
  314. }
  315. err2 = xfs_trans_commit(args.trans,
  316. XFS_TRANS_RELEASE_LOG_RES);
  317. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  318. return(error == 0 ? err2 : error);
  319. }
  320. /*
  321. * It won't fit in the shortform, transform to a leaf block.
  322. * GROT: another possible req'mt for a double-split btree op.
  323. */
  324. xfs_bmap_init(args.flist, args.firstblock);
  325. error = xfs_attr_shortform_to_leaf(&args);
  326. if (!error) {
  327. error = xfs_bmap_finish(&args.trans, args.flist,
  328. &committed);
  329. }
  330. if (error) {
  331. ASSERT(committed);
  332. args.trans = NULL;
  333. xfs_bmap_cancel(&flist);
  334. goto out;
  335. }
  336. /*
  337. * bmap_finish() may have committed the last trans and started
  338. * a new one. We need the inode to be in all transactions.
  339. */
  340. if (committed)
  341. xfs_trans_ijoin(args.trans, dp, 0);
  342. /*
  343. * Commit the leaf transformation. We'll need another (linked)
  344. * transaction to add the new attribute to the leaf.
  345. */
  346. error = xfs_trans_roll(&args.trans, dp);
  347. if (error)
  348. goto out;
  349. }
  350. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  351. error = xfs_attr_leaf_addname(&args);
  352. } else {
  353. error = xfs_attr_node_addname(&args);
  354. }
  355. if (error) {
  356. goto out;
  357. }
  358. /*
  359. * If this is a synchronous mount, make sure that the
  360. * transaction goes to disk before returning to the user.
  361. */
  362. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  363. xfs_trans_set_sync(args.trans);
  364. }
  365. if ((flags & ATTR_KERNOTIME) == 0)
  366. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  367. /*
  368. * Commit the last in the sequence of transactions.
  369. */
  370. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  371. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  372. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  373. return(error);
  374. out:
  375. if (args.trans)
  376. xfs_trans_cancel(args.trans,
  377. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  378. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  379. return(error);
  380. }
  381. int
  382. xfs_attr_set(
  383. xfs_inode_t *dp,
  384. const unsigned char *name,
  385. unsigned char *value,
  386. int valuelen,
  387. int flags)
  388. {
  389. int error;
  390. struct xfs_name xname;
  391. XFS_STATS_INC(xs_attr_set);
  392. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  393. return (EIO);
  394. error = xfs_attr_name_to_xname(&xname, name);
  395. if (error)
  396. return error;
  397. return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
  398. }
  399. /*
  400. * Generic handler routine to remove a name from an attribute list.
  401. * Transitions attribute list from Btree to shortform as necessary.
  402. */
  403. STATIC int
  404. xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
  405. {
  406. xfs_da_args_t args;
  407. xfs_fsblock_t firstblock;
  408. xfs_bmap_free_t flist;
  409. int error;
  410. xfs_mount_t *mp = dp->i_mount;
  411. /*
  412. * Fill in the arg structure for this request.
  413. */
  414. memset((char *)&args, 0, sizeof(args));
  415. args.name = name->name;
  416. args.namelen = name->len;
  417. args.flags = flags;
  418. args.hashval = xfs_da_hashname(args.name, args.namelen);
  419. args.dp = dp;
  420. args.firstblock = &firstblock;
  421. args.flist = &flist;
  422. args.total = 0;
  423. args.whichfork = XFS_ATTR_FORK;
  424. /*
  425. * we have no control over the attribute names that userspace passes us
  426. * to remove, so we have to allow the name lookup prior to attribute
  427. * removal to fail.
  428. */
  429. args.op_flags = XFS_DA_OP_OKNOENT;
  430. /*
  431. * Attach the dquots to the inode.
  432. */
  433. error = xfs_qm_dqattach(dp, 0);
  434. if (error)
  435. return error;
  436. /*
  437. * Start our first transaction of the day.
  438. *
  439. * All future transactions during this code must be "chained" off
  440. * this one via the trans_dup() call. All transactions will contain
  441. * the inode, and the inode will always be marked with trans_ihold().
  442. * Since the inode will be locked in all transactions, we must log
  443. * the inode in every transaction to let it float upward through
  444. * the log.
  445. */
  446. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
  447. /*
  448. * Root fork attributes can use reserved data blocks for this
  449. * operation if necessary
  450. */
  451. if (flags & ATTR_ROOT)
  452. args.trans->t_flags |= XFS_TRANS_RESERVE;
  453. if ((error = xfs_trans_reserve(args.trans,
  454. XFS_ATTRRM_SPACE_RES(mp),
  455. XFS_ATTRRM_LOG_RES(mp),
  456. 0, XFS_TRANS_PERM_LOG_RES,
  457. XFS_ATTRRM_LOG_COUNT))) {
  458. xfs_trans_cancel(args.trans, 0);
  459. return(error);
  460. }
  461. xfs_ilock(dp, XFS_ILOCK_EXCL);
  462. /*
  463. * No need to make quota reservations here. We expect to release some
  464. * blocks not allocate in the common case.
  465. */
  466. xfs_trans_ijoin(args.trans, dp, 0);
  467. /*
  468. * Decide on what work routines to call based on the inode size.
  469. */
  470. if (!xfs_inode_hasattr(dp)) {
  471. error = XFS_ERROR(ENOATTR);
  472. goto out;
  473. }
  474. if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  475. ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
  476. error = xfs_attr_shortform_remove(&args);
  477. if (error) {
  478. goto out;
  479. }
  480. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  481. error = xfs_attr_leaf_removename(&args);
  482. } else {
  483. error = xfs_attr_node_removename(&args);
  484. }
  485. if (error) {
  486. goto out;
  487. }
  488. /*
  489. * If this is a synchronous mount, make sure that the
  490. * transaction goes to disk before returning to the user.
  491. */
  492. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  493. xfs_trans_set_sync(args.trans);
  494. }
  495. if ((flags & ATTR_KERNOTIME) == 0)
  496. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  497. /*
  498. * Commit the last in the sequence of transactions.
  499. */
  500. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  501. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  502. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  503. return(error);
  504. out:
  505. if (args.trans)
  506. xfs_trans_cancel(args.trans,
  507. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  508. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  509. return(error);
  510. }
  511. int
  512. xfs_attr_remove(
  513. xfs_inode_t *dp,
  514. const unsigned char *name,
  515. int flags)
  516. {
  517. int error;
  518. struct xfs_name xname;
  519. XFS_STATS_INC(xs_attr_remove);
  520. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  521. return (EIO);
  522. error = xfs_attr_name_to_xname(&xname, name);
  523. if (error)
  524. return error;
  525. xfs_ilock(dp, XFS_ILOCK_SHARED);
  526. if (!xfs_inode_hasattr(dp)) {
  527. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  528. return XFS_ERROR(ENOATTR);
  529. }
  530. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  531. return xfs_attr_remove_int(dp, &xname, flags);
  532. }
  533. /*========================================================================
  534. * External routines when attribute list is inside the inode
  535. *========================================================================*/
  536. /*
  537. * Add a name to the shortform attribute list structure
  538. * This is the external routine.
  539. */
  540. STATIC int
  541. xfs_attr_shortform_addname(xfs_da_args_t *args)
  542. {
  543. int newsize, forkoff, retval;
  544. trace_xfs_attr_sf_addname(args);
  545. retval = xfs_attr_shortform_lookup(args);
  546. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  547. return(retval);
  548. } else if (retval == EEXIST) {
  549. if (args->flags & ATTR_CREATE)
  550. return(retval);
  551. retval = xfs_attr_shortform_remove(args);
  552. ASSERT(retval == 0);
  553. }
  554. if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
  555. args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  556. return(XFS_ERROR(ENOSPC));
  557. newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
  558. newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
  559. forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
  560. if (!forkoff)
  561. return(XFS_ERROR(ENOSPC));
  562. xfs_attr_shortform_add(args, forkoff);
  563. return(0);
  564. }
  565. /*========================================================================
  566. * External routines when attribute list is one block
  567. *========================================================================*/
  568. /*
  569. * Add a name to the leaf attribute list structure
  570. *
  571. * This leaf block cannot have a "remote" value, we only call this routine
  572. * if bmap_one_block() says there is only one block (ie: no remote blks).
  573. */
  574. STATIC int
  575. xfs_attr_leaf_addname(xfs_da_args_t *args)
  576. {
  577. xfs_inode_t *dp;
  578. struct xfs_buf *bp;
  579. int retval, error, committed, forkoff;
  580. trace_xfs_attr_leaf_addname(args);
  581. /*
  582. * Read the (only) block in the attribute list in.
  583. */
  584. dp = args->dp;
  585. args->blkno = 0;
  586. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  587. if (error)
  588. return error;
  589. /*
  590. * Look up the given attribute in the leaf block. Figure out if
  591. * the given flags produce an error or call for an atomic rename.
  592. */
  593. retval = xfs_attr3_leaf_lookup_int(bp, args);
  594. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  595. xfs_trans_brelse(args->trans, bp);
  596. return retval;
  597. } else if (retval == EEXIST) {
  598. if (args->flags & ATTR_CREATE) { /* pure create op */
  599. xfs_trans_brelse(args->trans, bp);
  600. return retval;
  601. }
  602. trace_xfs_attr_leaf_replace(args);
  603. args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
  604. args->blkno2 = args->blkno; /* set 2nd entry info*/
  605. args->index2 = args->index;
  606. args->rmtblkno2 = args->rmtblkno;
  607. args->rmtblkcnt2 = args->rmtblkcnt;
  608. }
  609. /*
  610. * Add the attribute to the leaf block, transitioning to a Btree
  611. * if required.
  612. */
  613. retval = xfs_attr3_leaf_add(bp, args);
  614. if (retval == ENOSPC) {
  615. /*
  616. * Promote the attribute list to the Btree format, then
  617. * Commit that transaction so that the node_addname() call
  618. * can manage its own transactions.
  619. */
  620. xfs_bmap_init(args->flist, args->firstblock);
  621. error = xfs_attr3_leaf_to_node(args);
  622. if (!error) {
  623. error = xfs_bmap_finish(&args->trans, args->flist,
  624. &committed);
  625. }
  626. if (error) {
  627. ASSERT(committed);
  628. args->trans = NULL;
  629. xfs_bmap_cancel(args->flist);
  630. return(error);
  631. }
  632. /*
  633. * bmap_finish() may have committed the last trans and started
  634. * a new one. We need the inode to be in all transactions.
  635. */
  636. if (committed)
  637. xfs_trans_ijoin(args->trans, dp, 0);
  638. /*
  639. * Commit the current trans (including the inode) and start
  640. * a new one.
  641. */
  642. error = xfs_trans_roll(&args->trans, dp);
  643. if (error)
  644. return (error);
  645. /*
  646. * Fob the whole rest of the problem off on the Btree code.
  647. */
  648. error = xfs_attr_node_addname(args);
  649. return(error);
  650. }
  651. /*
  652. * Commit the transaction that added the attr name so that
  653. * later routines can manage their own transactions.
  654. */
  655. error = xfs_trans_roll(&args->trans, dp);
  656. if (error)
  657. return (error);
  658. /*
  659. * If there was an out-of-line value, allocate the blocks we
  660. * identified for its storage and copy the value. This is done
  661. * after we create the attribute so that we don't overflow the
  662. * maximum size of a transaction and/or hit a deadlock.
  663. */
  664. if (args->rmtblkno > 0) {
  665. error = xfs_attr_rmtval_set(args);
  666. if (error)
  667. return(error);
  668. }
  669. /*
  670. * If this is an atomic rename operation, we must "flip" the
  671. * incomplete flags on the "new" and "old" attribute/value pairs
  672. * so that one disappears and one appears atomically. Then we
  673. * must remove the "old" attribute/value pair.
  674. */
  675. if (args->op_flags & XFS_DA_OP_RENAME) {
  676. /*
  677. * In a separate transaction, set the incomplete flag on the
  678. * "old" attr and clear the incomplete flag on the "new" attr.
  679. */
  680. error = xfs_attr3_leaf_flipflags(args);
  681. if (error)
  682. return(error);
  683. /*
  684. * Dismantle the "old" attribute/value pair by removing
  685. * a "remote" value (if it exists).
  686. */
  687. args->index = args->index2;
  688. args->blkno = args->blkno2;
  689. args->rmtblkno = args->rmtblkno2;
  690. args->rmtblkcnt = args->rmtblkcnt2;
  691. if (args->rmtblkno) {
  692. error = xfs_attr_rmtval_remove(args);
  693. if (error)
  694. return(error);
  695. }
  696. /*
  697. * Read in the block containing the "old" attr, then
  698. * remove the "old" attr from that block (neat, huh!)
  699. */
  700. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
  701. -1, &bp);
  702. if (error)
  703. return error;
  704. xfs_attr3_leaf_remove(bp, args);
  705. /*
  706. * If the result is small enough, shrink it all into the inode.
  707. */
  708. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  709. xfs_bmap_init(args->flist, args->firstblock);
  710. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  711. /* bp is gone due to xfs_da_shrink_inode */
  712. if (!error) {
  713. error = xfs_bmap_finish(&args->trans,
  714. args->flist,
  715. &committed);
  716. }
  717. if (error) {
  718. ASSERT(committed);
  719. args->trans = NULL;
  720. xfs_bmap_cancel(args->flist);
  721. return(error);
  722. }
  723. /*
  724. * bmap_finish() may have committed the last trans
  725. * and started a new one. We need the inode to be
  726. * in all transactions.
  727. */
  728. if (committed)
  729. xfs_trans_ijoin(args->trans, dp, 0);
  730. }
  731. /*
  732. * Commit the remove and start the next trans in series.
  733. */
  734. error = xfs_trans_roll(&args->trans, dp);
  735. } else if (args->rmtblkno > 0) {
  736. /*
  737. * Added a "remote" value, just clear the incomplete flag.
  738. */
  739. error = xfs_attr3_leaf_clearflag(args);
  740. }
  741. return error;
  742. }
  743. /*
  744. * Remove a name from the leaf attribute list structure
  745. *
  746. * This leaf block cannot have a "remote" value, we only call this routine
  747. * if bmap_one_block() says there is only one block (ie: no remote blks).
  748. */
  749. STATIC int
  750. xfs_attr_leaf_removename(xfs_da_args_t *args)
  751. {
  752. xfs_inode_t *dp;
  753. struct xfs_buf *bp;
  754. int error, committed, forkoff;
  755. trace_xfs_attr_leaf_removename(args);
  756. /*
  757. * Remove the attribute.
  758. */
  759. dp = args->dp;
  760. args->blkno = 0;
  761. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  762. if (error)
  763. return error;
  764. error = xfs_attr3_leaf_lookup_int(bp, args);
  765. if (error == ENOATTR) {
  766. xfs_trans_brelse(args->trans, bp);
  767. return error;
  768. }
  769. xfs_attr3_leaf_remove(bp, args);
  770. /*
  771. * If the result is small enough, shrink it all into the inode.
  772. */
  773. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  774. xfs_bmap_init(args->flist, args->firstblock);
  775. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  776. /* bp is gone due to xfs_da_shrink_inode */
  777. if (!error) {
  778. error = xfs_bmap_finish(&args->trans, args->flist,
  779. &committed);
  780. }
  781. if (error) {
  782. ASSERT(committed);
  783. args->trans = NULL;
  784. xfs_bmap_cancel(args->flist);
  785. return error;
  786. }
  787. /*
  788. * bmap_finish() may have committed the last trans and started
  789. * a new one. We need the inode to be in all transactions.
  790. */
  791. if (committed)
  792. xfs_trans_ijoin(args->trans, dp, 0);
  793. }
  794. return 0;
  795. }
  796. /*
  797. * Look up a name in a leaf attribute list structure.
  798. *
  799. * This leaf block cannot have a "remote" value, we only call this routine
  800. * if bmap_one_block() says there is only one block (ie: no remote blks).
  801. */
  802. STATIC int
  803. xfs_attr_leaf_get(xfs_da_args_t *args)
  804. {
  805. struct xfs_buf *bp;
  806. int error;
  807. trace_xfs_attr_leaf_get(args);
  808. args->blkno = 0;
  809. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  810. if (error)
  811. return error;
  812. error = xfs_attr3_leaf_lookup_int(bp, args);
  813. if (error != EEXIST) {
  814. xfs_trans_brelse(args->trans, bp);
  815. return error;
  816. }
  817. error = xfs_attr3_leaf_getvalue(bp, args);
  818. xfs_trans_brelse(args->trans, bp);
  819. if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
  820. error = xfs_attr_rmtval_get(args);
  821. }
  822. return error;
  823. }
  824. /*========================================================================
  825. * External routines when attribute list size > XFS_LBSIZE(mp).
  826. *========================================================================*/
  827. /*
  828. * Add a name to a Btree-format attribute list.
  829. *
  830. * This will involve walking down the Btree, and may involve splitting
  831. * leaf nodes and even splitting intermediate nodes up to and including
  832. * the root node (a special case of an intermediate node).
  833. *
  834. * "Remote" attribute values confuse the issue and atomic rename operations
  835. * add a whole extra layer of confusion on top of that.
  836. */
  837. STATIC int
  838. xfs_attr_node_addname(xfs_da_args_t *args)
  839. {
  840. xfs_da_state_t *state;
  841. xfs_da_state_blk_t *blk;
  842. xfs_inode_t *dp;
  843. xfs_mount_t *mp;
  844. int committed, retval, error;
  845. trace_xfs_attr_node_addname(args);
  846. /*
  847. * Fill in bucket of arguments/results/context to carry around.
  848. */
  849. dp = args->dp;
  850. mp = dp->i_mount;
  851. restart:
  852. state = xfs_da_state_alloc();
  853. state->args = args;
  854. state->mp = mp;
  855. state->blocksize = state->mp->m_sb.sb_blocksize;
  856. state->node_ents = state->mp->m_attr_node_ents;
  857. /*
  858. * Search to see if name already exists, and get back a pointer
  859. * to where it should go.
  860. */
  861. error = xfs_da3_node_lookup_int(state, &retval);
  862. if (error)
  863. goto out;
  864. blk = &state->path.blk[ state->path.active-1 ];
  865. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  866. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  867. goto out;
  868. } else if (retval == EEXIST) {
  869. if (args->flags & ATTR_CREATE)
  870. goto out;
  871. trace_xfs_attr_node_replace(args);
  872. args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
  873. args->blkno2 = args->blkno; /* set 2nd entry info*/
  874. args->index2 = args->index;
  875. args->rmtblkno2 = args->rmtblkno;
  876. args->rmtblkcnt2 = args->rmtblkcnt;
  877. args->rmtblkno = 0;
  878. args->rmtblkcnt = 0;
  879. }
  880. retval = xfs_attr3_leaf_add(blk->bp, state->args);
  881. if (retval == ENOSPC) {
  882. if (state->path.active == 1) {
  883. /*
  884. * Its really a single leaf node, but it had
  885. * out-of-line values so it looked like it *might*
  886. * have been a b-tree.
  887. */
  888. xfs_da_state_free(state);
  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. }