xfs_attr.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
  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_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.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_attr.h"
  37. #include "xfs_attr_leaf.h"
  38. #include "xfs_error.h"
  39. #include "xfs_quota.h"
  40. #include "xfs_trans_space.h"
  41. #include "xfs_rw.h"
  42. #include "xfs_vnodeops.h"
  43. #include "xfs_trace.h"
  44. /*
  45. * xfs_attr.c
  46. *
  47. * Provide the external interfaces to manage attribute lists.
  48. */
  49. /*========================================================================
  50. * Function prototypes for the kernel.
  51. *========================================================================*/
  52. /*
  53. * Internal routines when attribute list fits inside the inode.
  54. */
  55. STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
  56. /*
  57. * Internal routines when attribute list is one block.
  58. */
  59. STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
  60. STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
  61. STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
  62. STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
  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_node_list(xfs_attr_list_context_t *context);
  70. STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
  71. STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
  72. /*
  73. * Routines to manipulate out-of-line attribute values.
  74. */
  75. STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
  76. STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
  77. #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
  78. STATIC int
  79. xfs_attr_name_to_xname(
  80. struct xfs_name *xname,
  81. const unsigned char *aname)
  82. {
  83. if (!aname)
  84. return EINVAL;
  85. xname->name = aname;
  86. xname->len = strlen((char *)aname);
  87. if (xname->len >= MAXNAMELEN)
  88. return EFAULT; /* match IRIX behaviour */
  89. return 0;
  90. }
  91. STATIC int
  92. xfs_inode_hasattr(
  93. struct xfs_inode *ip)
  94. {
  95. if (!XFS_IFORK_Q(ip) ||
  96. (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  97. ip->i_d.di_anextents == 0))
  98. return 0;
  99. return 1;
  100. }
  101. /*========================================================================
  102. * Overall external interface routines.
  103. *========================================================================*/
  104. STATIC int
  105. xfs_attr_get_int(
  106. struct xfs_inode *ip,
  107. struct xfs_name *name,
  108. unsigned char *value,
  109. int *valuelenp,
  110. int flags)
  111. {
  112. xfs_da_args_t args;
  113. int error;
  114. if (!xfs_inode_hasattr(ip))
  115. return ENOATTR;
  116. /*
  117. * Fill in the arg structure for this request.
  118. */
  119. memset((char *)&args, 0, sizeof(args));
  120. args.name = name->name;
  121. args.namelen = name->len;
  122. args.value = value;
  123. args.valuelen = *valuelenp;
  124. args.flags = flags;
  125. args.hashval = xfs_da_hashname(args.name, args.namelen);
  126. args.dp = ip;
  127. args.whichfork = XFS_ATTR_FORK;
  128. /*
  129. * Decide on what work routines to call based on the inode size.
  130. */
  131. if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  132. error = xfs_attr_shortform_getvalue(&args);
  133. } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
  134. error = xfs_attr_leaf_get(&args);
  135. } else {
  136. error = xfs_attr_node_get(&args);
  137. }
  138. /*
  139. * Return the number of bytes in the value to the caller.
  140. */
  141. *valuelenp = args.valuelen;
  142. if (error == EEXIST)
  143. error = 0;
  144. return(error);
  145. }
  146. int
  147. xfs_attr_get(
  148. xfs_inode_t *ip,
  149. const unsigned char *name,
  150. unsigned char *value,
  151. int *valuelenp,
  152. int flags)
  153. {
  154. int error;
  155. struct xfs_name xname;
  156. XFS_STATS_INC(xs_attr_get);
  157. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  158. return(EIO);
  159. error = xfs_attr_name_to_xname(&xname, name);
  160. if (error)
  161. return error;
  162. xfs_ilock(ip, XFS_ILOCK_SHARED);
  163. error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags);
  164. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  165. return(error);
  166. }
  167. /*
  168. * Calculate how many blocks we need for the new attribute,
  169. */
  170. STATIC int
  171. xfs_attr_calc_size(
  172. struct xfs_inode *ip,
  173. int namelen,
  174. int valuelen,
  175. int *local)
  176. {
  177. struct xfs_mount *mp = ip->i_mount;
  178. int size;
  179. int nblks;
  180. /*
  181. * Determine space new attribute will use, and if it would be
  182. * "local" or "remote" (note: local != inline).
  183. */
  184. size = xfs_attr_leaf_newentsize(namelen, valuelen,
  185. mp->m_sb.sb_blocksize, local);
  186. nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
  187. if (*local) {
  188. if (size > (mp->m_sb.sb_blocksize >> 1)) {
  189. /* Double split possible */
  190. nblks *= 2;
  191. }
  192. } else {
  193. /*
  194. * Out of line attribute, cannot double split, but
  195. * make room for the attribute value itself.
  196. */
  197. uint dblocks = XFS_B_TO_FSB(mp, valuelen);
  198. nblks += dblocks;
  199. nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
  200. }
  201. return nblks;
  202. }
  203. STATIC int
  204. xfs_attr_set_int(
  205. struct xfs_inode *dp,
  206. struct xfs_name *name,
  207. unsigned char *value,
  208. int valuelen,
  209. int flags)
  210. {
  211. xfs_da_args_t args;
  212. xfs_fsblock_t firstblock;
  213. xfs_bmap_free_t flist;
  214. int error, err2, committed;
  215. xfs_mount_t *mp = dp->i_mount;
  216. int rsvd = (flags & ATTR_ROOT) != 0;
  217. int local;
  218. /*
  219. * Attach the dquots to the inode.
  220. */
  221. error = xfs_qm_dqattach(dp, 0);
  222. if (error)
  223. return error;
  224. /*
  225. * If the inode doesn't have an attribute fork, add one.
  226. * (inode must not be locked when we call this routine)
  227. */
  228. if (XFS_IFORK_Q(dp) == 0) {
  229. int sf_size = sizeof(xfs_attr_sf_hdr_t) +
  230. XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
  231. if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
  232. return(error);
  233. }
  234. /*
  235. * Fill in the arg structure for this request.
  236. */
  237. memset((char *)&args, 0, sizeof(args));
  238. args.name = name->name;
  239. args.namelen = name->len;
  240. args.value = value;
  241. args.valuelen = valuelen;
  242. args.flags = flags;
  243. args.hashval = xfs_da_hashname(args.name, args.namelen);
  244. args.dp = dp;
  245. args.firstblock = &firstblock;
  246. args.flist = &flist;
  247. args.whichfork = XFS_ATTR_FORK;
  248. args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  249. /* Size is now blocks for attribute data */
  250. args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
  251. /*
  252. * Start our first transaction of the day.
  253. *
  254. * All future transactions during this code must be "chained" off
  255. * this one via the trans_dup() call. All transactions will contain
  256. * the inode, and the inode will always be marked with trans_ihold().
  257. * Since the inode will be locked in all transactions, we must log
  258. * the inode in every transaction to let it float upward through
  259. * the log.
  260. */
  261. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
  262. /*
  263. * Root fork attributes can use reserved data blocks for this
  264. * operation if necessary
  265. */
  266. if (rsvd)
  267. args.trans->t_flags |= XFS_TRANS_RESERVE;
  268. if ((error = xfs_trans_reserve(args.trans, args.total,
  269. XFS_ATTRSET_LOG_RES(mp, args.total), 0,
  270. XFS_TRANS_PERM_LOG_RES, XFS_ATTRSET_LOG_COUNT))) {
  271. xfs_trans_cancel(args.trans, 0);
  272. return(error);
  273. }
  274. xfs_ilock(dp, XFS_ILOCK_EXCL);
  275. error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
  276. rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  277. XFS_QMOPT_RES_REGBLKS);
  278. if (error) {
  279. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  280. xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  281. return (error);
  282. }
  283. xfs_trans_ijoin(args.trans, dp);
  284. /*
  285. * If the attribute list is non-existent or a shortform list,
  286. * upgrade it to a single-leaf-block attribute list.
  287. */
  288. if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
  289. ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
  290. (dp->i_d.di_anextents == 0))) {
  291. /*
  292. * Build initial attribute list (if required).
  293. */
  294. if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
  295. xfs_attr_shortform_create(&args);
  296. /*
  297. * Try to add the attr to the attribute list in
  298. * the inode.
  299. */
  300. error = xfs_attr_shortform_addname(&args);
  301. if (error != ENOSPC) {
  302. /*
  303. * Commit the shortform mods, and we're done.
  304. * NOTE: this is also the error path (EEXIST, etc).
  305. */
  306. ASSERT(args.trans != NULL);
  307. /*
  308. * If this is a synchronous mount, make sure that
  309. * the transaction goes to disk before returning
  310. * to the user.
  311. */
  312. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  313. xfs_trans_set_sync(args.trans);
  314. }
  315. err2 = xfs_trans_commit(args.trans,
  316. XFS_TRANS_RELEASE_LOG_RES);
  317. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  318. /*
  319. * Hit the inode change time.
  320. */
  321. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  322. xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
  323. }
  324. return(error == 0 ? err2 : error);
  325. }
  326. /*
  327. * It won't fit in the shortform, transform to a leaf block.
  328. * GROT: another possible req'mt for a double-split btree op.
  329. */
  330. xfs_bmap_init(args.flist, args.firstblock);
  331. error = xfs_attr_shortform_to_leaf(&args);
  332. if (!error) {
  333. error = xfs_bmap_finish(&args.trans, args.flist,
  334. &committed);
  335. }
  336. if (error) {
  337. ASSERT(committed);
  338. args.trans = NULL;
  339. xfs_bmap_cancel(&flist);
  340. goto out;
  341. }
  342. /*
  343. * bmap_finish() may have committed the last trans and started
  344. * a new one. We need the inode to be in all transactions.
  345. */
  346. if (committed)
  347. xfs_trans_ijoin(args.trans, dp);
  348. /*
  349. * Commit the leaf transformation. We'll need another (linked)
  350. * transaction to add the new attribute to the leaf.
  351. */
  352. error = xfs_trans_roll(&args.trans, dp);
  353. if (error)
  354. goto out;
  355. }
  356. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  357. error = xfs_attr_leaf_addname(&args);
  358. } else {
  359. error = xfs_attr_node_addname(&args);
  360. }
  361. if (error) {
  362. goto out;
  363. }
  364. /*
  365. * If this is a synchronous mount, make sure that the
  366. * transaction goes to disk before returning to the user.
  367. */
  368. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  369. xfs_trans_set_sync(args.trans);
  370. }
  371. /*
  372. * Commit the last in the sequence of transactions.
  373. */
  374. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  375. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  376. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  377. /*
  378. * Hit the inode change time.
  379. */
  380. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  381. xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
  382. }
  383. return(error);
  384. out:
  385. if (args.trans)
  386. xfs_trans_cancel(args.trans,
  387. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  388. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  389. return(error);
  390. }
  391. int
  392. xfs_attr_set(
  393. xfs_inode_t *dp,
  394. const unsigned char *name,
  395. unsigned char *value,
  396. int valuelen,
  397. int flags)
  398. {
  399. int error;
  400. struct xfs_name xname;
  401. XFS_STATS_INC(xs_attr_set);
  402. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  403. return (EIO);
  404. error = xfs_attr_name_to_xname(&xname, name);
  405. if (error)
  406. return error;
  407. return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
  408. }
  409. /*
  410. * Generic handler routine to remove a name from an attribute list.
  411. * Transitions attribute list from Btree to shortform as necessary.
  412. */
  413. STATIC int
  414. xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
  415. {
  416. xfs_da_args_t args;
  417. xfs_fsblock_t firstblock;
  418. xfs_bmap_free_t flist;
  419. int error;
  420. xfs_mount_t *mp = dp->i_mount;
  421. /*
  422. * Fill in the arg structure for this request.
  423. */
  424. memset((char *)&args, 0, sizeof(args));
  425. args.name = name->name;
  426. args.namelen = name->len;
  427. args.flags = flags;
  428. args.hashval = xfs_da_hashname(args.name, args.namelen);
  429. args.dp = dp;
  430. args.firstblock = &firstblock;
  431. args.flist = &flist;
  432. args.total = 0;
  433. args.whichfork = XFS_ATTR_FORK;
  434. /*
  435. * Attach the dquots to the inode.
  436. */
  437. error = xfs_qm_dqattach(dp, 0);
  438. if (error)
  439. return error;
  440. /*
  441. * Start our first transaction of the day.
  442. *
  443. * All future transactions during this code must be "chained" off
  444. * this one via the trans_dup() call. All transactions will contain
  445. * the inode, and the inode will always be marked with trans_ihold().
  446. * Since the inode will be locked in all transactions, we must log
  447. * the inode in every transaction to let it float upward through
  448. * the log.
  449. */
  450. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
  451. /*
  452. * Root fork attributes can use reserved data blocks for this
  453. * operation if necessary
  454. */
  455. if (flags & ATTR_ROOT)
  456. args.trans->t_flags |= XFS_TRANS_RESERVE;
  457. if ((error = xfs_trans_reserve(args.trans,
  458. XFS_ATTRRM_SPACE_RES(mp),
  459. XFS_ATTRRM_LOG_RES(mp),
  460. 0, XFS_TRANS_PERM_LOG_RES,
  461. XFS_ATTRRM_LOG_COUNT))) {
  462. xfs_trans_cancel(args.trans, 0);
  463. return(error);
  464. }
  465. xfs_ilock(dp, XFS_ILOCK_EXCL);
  466. /*
  467. * No need to make quota reservations here. We expect to release some
  468. * blocks not allocate in the common case.
  469. */
  470. xfs_trans_ijoin(args.trans, dp);
  471. /*
  472. * Decide on what work routines to call based on the inode size.
  473. */
  474. if (!xfs_inode_hasattr(dp)) {
  475. error = XFS_ERROR(ENOATTR);
  476. goto out;
  477. }
  478. if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  479. ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
  480. error = xfs_attr_shortform_remove(&args);
  481. if (error) {
  482. goto out;
  483. }
  484. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  485. error = xfs_attr_leaf_removename(&args);
  486. } else {
  487. error = xfs_attr_node_removename(&args);
  488. }
  489. if (error) {
  490. goto out;
  491. }
  492. /*
  493. * If this is a synchronous mount, make sure that the
  494. * transaction goes to disk before returning to the user.
  495. */
  496. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  497. xfs_trans_set_sync(args.trans);
  498. }
  499. /*
  500. * Commit the last in the sequence of transactions.
  501. */
  502. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  503. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  504. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  505. /*
  506. * Hit the inode change time.
  507. */
  508. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  509. xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
  510. }
  511. return(error);
  512. out:
  513. if (args.trans)
  514. xfs_trans_cancel(args.trans,
  515. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  516. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  517. return(error);
  518. }
  519. int
  520. xfs_attr_remove(
  521. xfs_inode_t *dp,
  522. const unsigned char *name,
  523. int flags)
  524. {
  525. int error;
  526. struct xfs_name xname;
  527. XFS_STATS_INC(xs_attr_remove);
  528. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  529. return (EIO);
  530. error = xfs_attr_name_to_xname(&xname, name);
  531. if (error)
  532. return error;
  533. xfs_ilock(dp, XFS_ILOCK_SHARED);
  534. if (!xfs_inode_hasattr(dp)) {
  535. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  536. return XFS_ERROR(ENOATTR);
  537. }
  538. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  539. return xfs_attr_remove_int(dp, &xname, flags);
  540. }
  541. int
  542. xfs_attr_list_int(xfs_attr_list_context_t *context)
  543. {
  544. int error;
  545. xfs_inode_t *dp = context->dp;
  546. XFS_STATS_INC(xs_attr_list);
  547. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  548. return EIO;
  549. xfs_ilock(dp, XFS_ILOCK_SHARED);
  550. /*
  551. * Decide on what work routines to call based on the inode size.
  552. */
  553. if (!xfs_inode_hasattr(dp)) {
  554. error = 0;
  555. } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  556. error = xfs_attr_shortform_list(context);
  557. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  558. error = xfs_attr_leaf_list(context);
  559. } else {
  560. error = xfs_attr_node_list(context);
  561. }
  562. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  563. return error;
  564. }
  565. #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
  566. (((struct attrlist_ent *) 0)->a_name - (char *) 0)
  567. #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
  568. ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
  569. & ~(sizeof(u_int32_t)-1))
  570. /*
  571. * Format an attribute and copy it out to the user's buffer.
  572. * Take care to check values and protect against them changing later,
  573. * we may be reading them directly out of a user buffer.
  574. */
  575. /*ARGSUSED*/
  576. STATIC int
  577. xfs_attr_put_listent(
  578. xfs_attr_list_context_t *context,
  579. int flags,
  580. unsigned char *name,
  581. int namelen,
  582. int valuelen,
  583. unsigned char *value)
  584. {
  585. struct attrlist *alist = (struct attrlist *)context->alist;
  586. attrlist_ent_t *aep;
  587. int arraytop;
  588. ASSERT(!(context->flags & ATTR_KERNOVAL));
  589. ASSERT(context->count >= 0);
  590. ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
  591. ASSERT(context->firstu >= sizeof(*alist));
  592. ASSERT(context->firstu <= context->bufsize);
  593. /*
  594. * Only list entries in the right namespace.
  595. */
  596. if (((context->flags & ATTR_SECURE) == 0) !=
  597. ((flags & XFS_ATTR_SECURE) == 0))
  598. return 0;
  599. if (((context->flags & ATTR_ROOT) == 0) !=
  600. ((flags & XFS_ATTR_ROOT) == 0))
  601. return 0;
  602. arraytop = sizeof(*alist) +
  603. context->count * sizeof(alist->al_offset[0]);
  604. context->firstu -= ATTR_ENTSIZE(namelen);
  605. if (context->firstu < arraytop) {
  606. trace_xfs_attr_list_full(context);
  607. alist->al_more = 1;
  608. context->seen_enough = 1;
  609. return 1;
  610. }
  611. aep = (attrlist_ent_t *)&context->alist[context->firstu];
  612. aep->a_valuelen = valuelen;
  613. memcpy(aep->a_name, name, namelen);
  614. aep->a_name[namelen] = 0;
  615. alist->al_offset[context->count++] = context->firstu;
  616. alist->al_count = context->count;
  617. trace_xfs_attr_list_add(context);
  618. return 0;
  619. }
  620. /*
  621. * Generate a list of extended attribute names and optionally
  622. * also value lengths. Positive return value follows the XFS
  623. * convention of being an error, zero or negative return code
  624. * is the length of the buffer returned (negated), indicating
  625. * success.
  626. */
  627. int
  628. xfs_attr_list(
  629. xfs_inode_t *dp,
  630. char *buffer,
  631. int bufsize,
  632. int flags,
  633. attrlist_cursor_kern_t *cursor)
  634. {
  635. xfs_attr_list_context_t context;
  636. struct attrlist *alist;
  637. int error;
  638. /*
  639. * Validate the cursor.
  640. */
  641. if (cursor->pad1 || cursor->pad2)
  642. return(XFS_ERROR(EINVAL));
  643. if ((cursor->initted == 0) &&
  644. (cursor->hashval || cursor->blkno || cursor->offset))
  645. return XFS_ERROR(EINVAL);
  646. /*
  647. * Check for a properly aligned buffer.
  648. */
  649. if (((long)buffer) & (sizeof(int)-1))
  650. return XFS_ERROR(EFAULT);
  651. if (flags & ATTR_KERNOVAL)
  652. bufsize = 0;
  653. /*
  654. * Initialize the output buffer.
  655. */
  656. memset(&context, 0, sizeof(context));
  657. context.dp = dp;
  658. context.cursor = cursor;
  659. context.resynch = 1;
  660. context.flags = flags;
  661. context.alist = buffer;
  662. context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
  663. context.firstu = context.bufsize;
  664. context.put_listent = xfs_attr_put_listent;
  665. alist = (struct attrlist *)context.alist;
  666. alist->al_count = 0;
  667. alist->al_more = 0;
  668. alist->al_offset[0] = context.bufsize;
  669. error = xfs_attr_list_int(&context);
  670. ASSERT(error >= 0);
  671. return error;
  672. }
  673. int /* error */
  674. xfs_attr_inactive(xfs_inode_t *dp)
  675. {
  676. xfs_trans_t *trans;
  677. xfs_mount_t *mp;
  678. int error;
  679. mp = dp->i_mount;
  680. ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
  681. xfs_ilock(dp, XFS_ILOCK_SHARED);
  682. if (!xfs_inode_hasattr(dp) ||
  683. dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  684. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  685. return 0;
  686. }
  687. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  688. /*
  689. * Start our first transaction of the day.
  690. *
  691. * All future transactions during this code must be "chained" off
  692. * this one via the trans_dup() call. All transactions will contain
  693. * the inode, and the inode will always be marked with trans_ihold().
  694. * Since the inode will be locked in all transactions, we must log
  695. * the inode in every transaction to let it float upward through
  696. * the log.
  697. */
  698. trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
  699. if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
  700. XFS_TRANS_PERM_LOG_RES,
  701. XFS_ATTRINVAL_LOG_COUNT))) {
  702. xfs_trans_cancel(trans, 0);
  703. return(error);
  704. }
  705. xfs_ilock(dp, XFS_ILOCK_EXCL);
  706. /*
  707. * No need to make quota reservations here. We expect to release some
  708. * blocks, not allocate, in the common case.
  709. */
  710. xfs_trans_ijoin(trans, dp);
  711. /*
  712. * Decide on what work routines to call based on the inode size.
  713. */
  714. if (!xfs_inode_hasattr(dp) ||
  715. dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  716. error = 0;
  717. goto out;
  718. }
  719. error = xfs_attr_root_inactive(&trans, dp);
  720. if (error)
  721. goto out;
  722. /*
  723. * signal synchronous inactive transactions unless this
  724. * is a synchronous mount filesystem in which case we
  725. * know that we're here because we've been called out of
  726. * xfs_inactive which means that the last reference is gone
  727. * and the unlink transaction has already hit the disk so
  728. * async inactive transactions are safe.
  729. */
  730. if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
  731. (!(mp->m_flags & XFS_MOUNT_WSYNC)
  732. ? 1 : 0))))
  733. goto out;
  734. /*
  735. * Commit the last in the sequence of transactions.
  736. */
  737. xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
  738. error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
  739. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  740. return(error);
  741. out:
  742. xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  743. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  744. return(error);
  745. }
  746. /*========================================================================
  747. * External routines when attribute list is inside the inode
  748. *========================================================================*/
  749. /*
  750. * Add a name to the shortform attribute list structure
  751. * This is the external routine.
  752. */
  753. STATIC int
  754. xfs_attr_shortform_addname(xfs_da_args_t *args)
  755. {
  756. int newsize, forkoff, retval;
  757. retval = xfs_attr_shortform_lookup(args);
  758. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  759. return(retval);
  760. } else if (retval == EEXIST) {
  761. if (args->flags & ATTR_CREATE)
  762. return(retval);
  763. retval = xfs_attr_shortform_remove(args);
  764. ASSERT(retval == 0);
  765. }
  766. if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
  767. args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  768. return(XFS_ERROR(ENOSPC));
  769. newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
  770. newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
  771. forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
  772. if (!forkoff)
  773. return(XFS_ERROR(ENOSPC));
  774. xfs_attr_shortform_add(args, forkoff);
  775. return(0);
  776. }
  777. /*========================================================================
  778. * External routines when attribute list is one block
  779. *========================================================================*/
  780. /*
  781. * Add a name to the leaf attribute list structure
  782. *
  783. * This leaf block cannot have a "remote" value, we only call this routine
  784. * if bmap_one_block() says there is only one block (ie: no remote blks).
  785. */
  786. STATIC int
  787. xfs_attr_leaf_addname(xfs_da_args_t *args)
  788. {
  789. xfs_inode_t *dp;
  790. xfs_dabuf_t *bp;
  791. int retval, error, committed, forkoff;
  792. /*
  793. * Read the (only) block in the attribute list in.
  794. */
  795. dp = args->dp;
  796. args->blkno = 0;
  797. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  798. XFS_ATTR_FORK);
  799. if (error)
  800. return(error);
  801. ASSERT(bp != NULL);
  802. /*
  803. * Look up the given attribute in the leaf block. Figure out if
  804. * the given flags produce an error or call for an atomic rename.
  805. */
  806. retval = xfs_attr_leaf_lookup_int(bp, args);
  807. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  808. xfs_da_brelse(args->trans, bp);
  809. return(retval);
  810. } else if (retval == EEXIST) {
  811. if (args->flags & ATTR_CREATE) { /* pure create op */
  812. xfs_da_brelse(args->trans, bp);
  813. return(retval);
  814. }
  815. args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
  816. args->blkno2 = args->blkno; /* set 2nd entry info*/
  817. args->index2 = args->index;
  818. args->rmtblkno2 = args->rmtblkno;
  819. args->rmtblkcnt2 = args->rmtblkcnt;
  820. }
  821. /*
  822. * Add the attribute to the leaf block, transitioning to a Btree
  823. * if required.
  824. */
  825. retval = xfs_attr_leaf_add(bp, args);
  826. xfs_da_buf_done(bp);
  827. if (retval == ENOSPC) {
  828. /*
  829. * Promote the attribute list to the Btree format, then
  830. * Commit that transaction so that the node_addname() call
  831. * can manage its own transactions.
  832. */
  833. xfs_bmap_init(args->flist, args->firstblock);
  834. error = xfs_attr_leaf_to_node(args);
  835. if (!error) {
  836. error = xfs_bmap_finish(&args->trans, args->flist,
  837. &committed);
  838. }
  839. if (error) {
  840. ASSERT(committed);
  841. args->trans = NULL;
  842. xfs_bmap_cancel(args->flist);
  843. return(error);
  844. }
  845. /*
  846. * bmap_finish() may have committed the last trans and started
  847. * a new one. We need the inode to be in all transactions.
  848. */
  849. if (committed)
  850. xfs_trans_ijoin(args->trans, dp);
  851. /*
  852. * Commit the current trans (including the inode) and start
  853. * a new one.
  854. */
  855. error = xfs_trans_roll(&args->trans, dp);
  856. if (error)
  857. return (error);
  858. /*
  859. * Fob the whole rest of the problem off on the Btree code.
  860. */
  861. error = xfs_attr_node_addname(args);
  862. return(error);
  863. }
  864. /*
  865. * Commit the transaction that added the attr name so that
  866. * later routines can manage their own transactions.
  867. */
  868. error = xfs_trans_roll(&args->trans, dp);
  869. if (error)
  870. return (error);
  871. /*
  872. * If there was an out-of-line value, allocate the blocks we
  873. * identified for its storage and copy the value. This is done
  874. * after we create the attribute so that we don't overflow the
  875. * maximum size of a transaction and/or hit a deadlock.
  876. */
  877. if (args->rmtblkno > 0) {
  878. error = xfs_attr_rmtval_set(args);
  879. if (error)
  880. return(error);
  881. }
  882. /*
  883. * If this is an atomic rename operation, we must "flip" the
  884. * incomplete flags on the "new" and "old" attribute/value pairs
  885. * so that one disappears and one appears atomically. Then we
  886. * must remove the "old" attribute/value pair.
  887. */
  888. if (args->op_flags & XFS_DA_OP_RENAME) {
  889. /*
  890. * In a separate transaction, set the incomplete flag on the
  891. * "old" attr and clear the incomplete flag on the "new" attr.
  892. */
  893. error = xfs_attr_leaf_flipflags(args);
  894. if (error)
  895. return(error);
  896. /*
  897. * Dismantle the "old" attribute/value pair by removing
  898. * a "remote" value (if it exists).
  899. */
  900. args->index = args->index2;
  901. args->blkno = args->blkno2;
  902. args->rmtblkno = args->rmtblkno2;
  903. args->rmtblkcnt = args->rmtblkcnt2;
  904. if (args->rmtblkno) {
  905. error = xfs_attr_rmtval_remove(args);
  906. if (error)
  907. return(error);
  908. }
  909. /*
  910. * Read in the block containing the "old" attr, then
  911. * remove the "old" attr from that block (neat, huh!)
  912. */
  913. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
  914. &bp, XFS_ATTR_FORK);
  915. if (error)
  916. return(error);
  917. ASSERT(bp != NULL);
  918. (void)xfs_attr_leaf_remove(bp, args);
  919. /*
  920. * If the result is small enough, shrink it all into the inode.
  921. */
  922. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  923. xfs_bmap_init(args->flist, args->firstblock);
  924. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  925. /* bp is gone due to xfs_da_shrink_inode */
  926. if (!error) {
  927. error = xfs_bmap_finish(&args->trans,
  928. args->flist,
  929. &committed);
  930. }
  931. if (error) {
  932. ASSERT(committed);
  933. args->trans = NULL;
  934. xfs_bmap_cancel(args->flist);
  935. return(error);
  936. }
  937. /*
  938. * bmap_finish() may have committed the last trans
  939. * and started a new one. We need the inode to be
  940. * in all transactions.
  941. */
  942. if (committed)
  943. xfs_trans_ijoin(args->trans, dp);
  944. } else
  945. xfs_da_buf_done(bp);
  946. /*
  947. * Commit the remove and start the next trans in series.
  948. */
  949. error = xfs_trans_roll(&args->trans, dp);
  950. } else if (args->rmtblkno > 0) {
  951. /*
  952. * Added a "remote" value, just clear the incomplete flag.
  953. */
  954. error = xfs_attr_leaf_clearflag(args);
  955. }
  956. return(error);
  957. }
  958. /*
  959. * Remove a name from the leaf attribute list structure
  960. *
  961. * This leaf block cannot have a "remote" value, we only call this routine
  962. * if bmap_one_block() says there is only one block (ie: no remote blks).
  963. */
  964. STATIC int
  965. xfs_attr_leaf_removename(xfs_da_args_t *args)
  966. {
  967. xfs_inode_t *dp;
  968. xfs_dabuf_t *bp;
  969. int error, committed, forkoff;
  970. /*
  971. * Remove the attribute.
  972. */
  973. dp = args->dp;
  974. args->blkno = 0;
  975. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  976. XFS_ATTR_FORK);
  977. if (error) {
  978. return(error);
  979. }
  980. ASSERT(bp != NULL);
  981. error = xfs_attr_leaf_lookup_int(bp, args);
  982. if (error == ENOATTR) {
  983. xfs_da_brelse(args->trans, bp);
  984. return(error);
  985. }
  986. (void)xfs_attr_leaf_remove(bp, args);
  987. /*
  988. * If the result is small enough, shrink it all into the inode.
  989. */
  990. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  991. xfs_bmap_init(args->flist, args->firstblock);
  992. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  993. /* bp is gone due to xfs_da_shrink_inode */
  994. if (!error) {
  995. error = xfs_bmap_finish(&args->trans, args->flist,
  996. &committed);
  997. }
  998. if (error) {
  999. ASSERT(committed);
  1000. args->trans = NULL;
  1001. xfs_bmap_cancel(args->flist);
  1002. return(error);
  1003. }
  1004. /*
  1005. * bmap_finish() may have committed the last trans and started
  1006. * a new one. We need the inode to be in all transactions.
  1007. */
  1008. if (committed)
  1009. xfs_trans_ijoin(args->trans, dp);
  1010. } else
  1011. xfs_da_buf_done(bp);
  1012. return(0);
  1013. }
  1014. /*
  1015. * Look up a name in a leaf attribute list structure.
  1016. *
  1017. * This leaf block cannot have a "remote" value, we only call this routine
  1018. * if bmap_one_block() says there is only one block (ie: no remote blks).
  1019. */
  1020. STATIC int
  1021. xfs_attr_leaf_get(xfs_da_args_t *args)
  1022. {
  1023. xfs_dabuf_t *bp;
  1024. int error;
  1025. args->blkno = 0;
  1026. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  1027. XFS_ATTR_FORK);
  1028. if (error)
  1029. return(error);
  1030. ASSERT(bp != NULL);
  1031. error = xfs_attr_leaf_lookup_int(bp, args);
  1032. if (error != EEXIST) {
  1033. xfs_da_brelse(args->trans, bp);
  1034. return(error);
  1035. }
  1036. error = xfs_attr_leaf_getvalue(bp, args);
  1037. xfs_da_brelse(args->trans, bp);
  1038. if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
  1039. error = xfs_attr_rmtval_get(args);
  1040. }
  1041. return(error);
  1042. }
  1043. /*
  1044. * Copy out attribute entries for attr_list(), for leaf attribute lists.
  1045. */
  1046. STATIC int
  1047. xfs_attr_leaf_list(xfs_attr_list_context_t *context)
  1048. {
  1049. xfs_attr_leafblock_t *leaf;
  1050. int error;
  1051. xfs_dabuf_t *bp;
  1052. context->cursor->blkno = 0;
  1053. error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
  1054. if (error)
  1055. return XFS_ERROR(error);
  1056. ASSERT(bp != NULL);
  1057. leaf = bp->data;
  1058. if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC)) {
  1059. XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
  1060. context->dp->i_mount, leaf);
  1061. xfs_da_brelse(NULL, bp);
  1062. return XFS_ERROR(EFSCORRUPTED);
  1063. }
  1064. error = xfs_attr_leaf_list_int(bp, context);
  1065. xfs_da_brelse(NULL, bp);
  1066. return XFS_ERROR(error);
  1067. }
  1068. /*========================================================================
  1069. * External routines when attribute list size > XFS_LBSIZE(mp).
  1070. *========================================================================*/
  1071. /*
  1072. * Add a name to a Btree-format attribute list.
  1073. *
  1074. * This will involve walking down the Btree, and may involve splitting
  1075. * leaf nodes and even splitting intermediate nodes up to and including
  1076. * the root node (a special case of an intermediate node).
  1077. *
  1078. * "Remote" attribute values confuse the issue and atomic rename operations
  1079. * add a whole extra layer of confusion on top of that.
  1080. */
  1081. STATIC int
  1082. xfs_attr_node_addname(xfs_da_args_t *args)
  1083. {
  1084. xfs_da_state_t *state;
  1085. xfs_da_state_blk_t *blk;
  1086. xfs_inode_t *dp;
  1087. xfs_mount_t *mp;
  1088. int committed, retval, error;
  1089. /*
  1090. * Fill in bucket of arguments/results/context to carry around.
  1091. */
  1092. dp = args->dp;
  1093. mp = dp->i_mount;
  1094. restart:
  1095. state = xfs_da_state_alloc();
  1096. state->args = args;
  1097. state->mp = mp;
  1098. state->blocksize = state->mp->m_sb.sb_blocksize;
  1099. state->node_ents = state->mp->m_attr_node_ents;
  1100. /*
  1101. * Search to see if name already exists, and get back a pointer
  1102. * to where it should go.
  1103. */
  1104. error = xfs_da_node_lookup_int(state, &retval);
  1105. if (error)
  1106. goto out;
  1107. blk = &state->path.blk[ state->path.active-1 ];
  1108. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1109. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  1110. goto out;
  1111. } else if (retval == EEXIST) {
  1112. if (args->flags & ATTR_CREATE)
  1113. goto out;
  1114. args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
  1115. args->blkno2 = args->blkno; /* set 2nd entry info*/
  1116. args->index2 = args->index;
  1117. args->rmtblkno2 = args->rmtblkno;
  1118. args->rmtblkcnt2 = args->rmtblkcnt;
  1119. args->rmtblkno = 0;
  1120. args->rmtblkcnt = 0;
  1121. }
  1122. retval = xfs_attr_leaf_add(blk->bp, state->args);
  1123. if (retval == ENOSPC) {
  1124. if (state->path.active == 1) {
  1125. /*
  1126. * Its really a single leaf node, but it had
  1127. * out-of-line values so it looked like it *might*
  1128. * have been a b-tree.
  1129. */
  1130. xfs_da_state_free(state);
  1131. xfs_bmap_init(args->flist, args->firstblock);
  1132. error = xfs_attr_leaf_to_node(args);
  1133. if (!error) {
  1134. error = xfs_bmap_finish(&args->trans,
  1135. args->flist,
  1136. &committed);
  1137. }
  1138. if (error) {
  1139. ASSERT(committed);
  1140. args->trans = NULL;
  1141. xfs_bmap_cancel(args->flist);
  1142. goto out;
  1143. }
  1144. /*
  1145. * bmap_finish() may have committed the last trans
  1146. * and started a new one. We need the inode to be
  1147. * in all transactions.
  1148. */
  1149. if (committed)
  1150. xfs_trans_ijoin(args->trans, dp);
  1151. /*
  1152. * Commit the node conversion and start the next
  1153. * trans in the chain.
  1154. */
  1155. error = xfs_trans_roll(&args->trans, dp);
  1156. if (error)
  1157. goto out;
  1158. goto restart;
  1159. }
  1160. /*
  1161. * Split as many Btree elements as required.
  1162. * This code tracks the new and old attr's location
  1163. * in the index/blkno/rmtblkno/rmtblkcnt fields and
  1164. * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
  1165. */
  1166. xfs_bmap_init(args->flist, args->firstblock);
  1167. error = xfs_da_split(state);
  1168. if (!error) {
  1169. error = xfs_bmap_finish(&args->trans, args->flist,
  1170. &committed);
  1171. }
  1172. if (error) {
  1173. ASSERT(committed);
  1174. args->trans = NULL;
  1175. xfs_bmap_cancel(args->flist);
  1176. goto out;
  1177. }
  1178. /*
  1179. * bmap_finish() may have committed the last trans and started
  1180. * a new one. We need the inode to be in all transactions.
  1181. */
  1182. if (committed)
  1183. xfs_trans_ijoin(args->trans, dp);
  1184. } else {
  1185. /*
  1186. * Addition succeeded, update Btree hashvals.
  1187. */
  1188. xfs_da_fixhashpath(state, &state->path);
  1189. }
  1190. /*
  1191. * Kill the state structure, we're done with it and need to
  1192. * allow the buffers to come back later.
  1193. */
  1194. xfs_da_state_free(state);
  1195. state = NULL;
  1196. /*
  1197. * Commit the leaf addition or btree split and start the next
  1198. * trans in the chain.
  1199. */
  1200. error = xfs_trans_roll(&args->trans, dp);
  1201. if (error)
  1202. goto out;
  1203. /*
  1204. * If there was an out-of-line value, allocate the blocks we
  1205. * identified for its storage and copy the value. This is done
  1206. * after we create the attribute so that we don't overflow the
  1207. * maximum size of a transaction and/or hit a deadlock.
  1208. */
  1209. if (args->rmtblkno > 0) {
  1210. error = xfs_attr_rmtval_set(args);
  1211. if (error)
  1212. return(error);
  1213. }
  1214. /*
  1215. * If this is an atomic rename operation, we must "flip" the
  1216. * incomplete flags on the "new" and "old" attribute/value pairs
  1217. * so that one disappears and one appears atomically. Then we
  1218. * must remove the "old" attribute/value pair.
  1219. */
  1220. if (args->op_flags & XFS_DA_OP_RENAME) {
  1221. /*
  1222. * In a separate transaction, set the incomplete flag on the
  1223. * "old" attr and clear the incomplete flag on the "new" attr.
  1224. */
  1225. error = xfs_attr_leaf_flipflags(args);
  1226. if (error)
  1227. goto out;
  1228. /*
  1229. * Dismantle the "old" attribute/value pair by removing
  1230. * a "remote" value (if it exists).
  1231. */
  1232. args->index = args->index2;
  1233. args->blkno = args->blkno2;
  1234. args->rmtblkno = args->rmtblkno2;
  1235. args->rmtblkcnt = args->rmtblkcnt2;
  1236. if (args->rmtblkno) {
  1237. error = xfs_attr_rmtval_remove(args);
  1238. if (error)
  1239. return(error);
  1240. }
  1241. /*
  1242. * Re-find the "old" attribute entry after any split ops.
  1243. * The INCOMPLETE flag means that we will find the "old"
  1244. * attr, not the "new" one.
  1245. */
  1246. args->flags |= XFS_ATTR_INCOMPLETE;
  1247. state = xfs_da_state_alloc();
  1248. state->args = args;
  1249. state->mp = mp;
  1250. state->blocksize = state->mp->m_sb.sb_blocksize;
  1251. state->node_ents = state->mp->m_attr_node_ents;
  1252. state->inleaf = 0;
  1253. error = xfs_da_node_lookup_int(state, &retval);
  1254. if (error)
  1255. goto out;
  1256. /*
  1257. * Remove the name and update the hashvals in the tree.
  1258. */
  1259. blk = &state->path.blk[ state->path.active-1 ];
  1260. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1261. error = xfs_attr_leaf_remove(blk->bp, args);
  1262. xfs_da_fixhashpath(state, &state->path);
  1263. /*
  1264. * Check to see if the tree needs to be collapsed.
  1265. */
  1266. if (retval && (state->path.active > 1)) {
  1267. xfs_bmap_init(args->flist, args->firstblock);
  1268. error = xfs_da_join(state);
  1269. if (!error) {
  1270. error = xfs_bmap_finish(&args->trans,
  1271. args->flist,
  1272. &committed);
  1273. }
  1274. if (error) {
  1275. ASSERT(committed);
  1276. args->trans = NULL;
  1277. xfs_bmap_cancel(args->flist);
  1278. goto out;
  1279. }
  1280. /*
  1281. * bmap_finish() may have committed the last trans
  1282. * and started a new one. We need the inode to be
  1283. * in all transactions.
  1284. */
  1285. if (committed)
  1286. xfs_trans_ijoin(args->trans, dp);
  1287. }
  1288. /*
  1289. * Commit and start the next trans in the chain.
  1290. */
  1291. error = xfs_trans_roll(&args->trans, dp);
  1292. if (error)
  1293. goto out;
  1294. } else if (args->rmtblkno > 0) {
  1295. /*
  1296. * Added a "remote" value, just clear the incomplete flag.
  1297. */
  1298. error = xfs_attr_leaf_clearflag(args);
  1299. if (error)
  1300. goto out;
  1301. }
  1302. retval = error = 0;
  1303. out:
  1304. if (state)
  1305. xfs_da_state_free(state);
  1306. if (error)
  1307. return(error);
  1308. return(retval);
  1309. }
  1310. /*
  1311. * Remove a name from a B-tree attribute list.
  1312. *
  1313. * This will involve walking down the Btree, and may involve joining
  1314. * leaf nodes and even joining intermediate nodes up to and including
  1315. * the root node (a special case of an intermediate node).
  1316. */
  1317. STATIC int
  1318. xfs_attr_node_removename(xfs_da_args_t *args)
  1319. {
  1320. xfs_da_state_t *state;
  1321. xfs_da_state_blk_t *blk;
  1322. xfs_inode_t *dp;
  1323. xfs_dabuf_t *bp;
  1324. int retval, error, committed, forkoff;
  1325. /*
  1326. * Tie a string around our finger to remind us where we are.
  1327. */
  1328. dp = args->dp;
  1329. state = xfs_da_state_alloc();
  1330. state->args = args;
  1331. state->mp = dp->i_mount;
  1332. state->blocksize = state->mp->m_sb.sb_blocksize;
  1333. state->node_ents = state->mp->m_attr_node_ents;
  1334. /*
  1335. * Search to see if name exists, and get back a pointer to it.
  1336. */
  1337. error = xfs_da_node_lookup_int(state, &retval);
  1338. if (error || (retval != EEXIST)) {
  1339. if (error == 0)
  1340. error = retval;
  1341. goto out;
  1342. }
  1343. /*
  1344. * If there is an out-of-line value, de-allocate the blocks.
  1345. * This is done before we remove the attribute so that we don't
  1346. * overflow the maximum size of a transaction and/or hit a deadlock.
  1347. */
  1348. blk = &state->path.blk[ state->path.active-1 ];
  1349. ASSERT(blk->bp != NULL);
  1350. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1351. if (args->rmtblkno > 0) {
  1352. /*
  1353. * Fill in disk block numbers in the state structure
  1354. * so that we can get the buffers back after we commit
  1355. * several transactions in the following calls.
  1356. */
  1357. error = xfs_attr_fillstate(state);
  1358. if (error)
  1359. goto out;
  1360. /*
  1361. * Mark the attribute as INCOMPLETE, then bunmapi() the
  1362. * remote value.
  1363. */
  1364. error = xfs_attr_leaf_setflag(args);
  1365. if (error)
  1366. goto out;
  1367. error = xfs_attr_rmtval_remove(args);
  1368. if (error)
  1369. goto out;
  1370. /*
  1371. * Refill the state structure with buffers, the prior calls
  1372. * released our buffers.
  1373. */
  1374. error = xfs_attr_refillstate(state);
  1375. if (error)
  1376. goto out;
  1377. }
  1378. /*
  1379. * Remove the name and update the hashvals in the tree.
  1380. */
  1381. blk = &state->path.blk[ state->path.active-1 ];
  1382. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1383. retval = xfs_attr_leaf_remove(blk->bp, args);
  1384. xfs_da_fixhashpath(state, &state->path);
  1385. /*
  1386. * Check to see if the tree needs to be collapsed.
  1387. */
  1388. if (retval && (state->path.active > 1)) {
  1389. xfs_bmap_init(args->flist, args->firstblock);
  1390. error = xfs_da_join(state);
  1391. if (!error) {
  1392. error = xfs_bmap_finish(&args->trans, args->flist,
  1393. &committed);
  1394. }
  1395. if (error) {
  1396. ASSERT(committed);
  1397. args->trans = NULL;
  1398. xfs_bmap_cancel(args->flist);
  1399. goto out;
  1400. }
  1401. /*
  1402. * bmap_finish() may have committed the last trans and started
  1403. * a new one. We need the inode to be in all transactions.
  1404. */
  1405. if (committed)
  1406. xfs_trans_ijoin(args->trans, dp);
  1407. /*
  1408. * Commit the Btree join operation and start a new trans.
  1409. */
  1410. error = xfs_trans_roll(&args->trans, dp);
  1411. if (error)
  1412. goto out;
  1413. }
  1414. /*
  1415. * If the result is small enough, push it all into the inode.
  1416. */
  1417. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  1418. /*
  1419. * Have to get rid of the copy of this dabuf in the state.
  1420. */
  1421. ASSERT(state->path.active == 1);
  1422. ASSERT(state->path.blk[0].bp);
  1423. xfs_da_buf_done(state->path.blk[0].bp);
  1424. state->path.blk[0].bp = NULL;
  1425. error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
  1426. XFS_ATTR_FORK);
  1427. if (error)
  1428. goto out;
  1429. ASSERT(be16_to_cpu(((xfs_attr_leafblock_t *)
  1430. bp->data)->hdr.info.magic)
  1431. == XFS_ATTR_LEAF_MAGIC);
  1432. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  1433. xfs_bmap_init(args->flist, args->firstblock);
  1434. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  1435. /* bp is gone due to xfs_da_shrink_inode */
  1436. if (!error) {
  1437. error = xfs_bmap_finish(&args->trans,
  1438. args->flist,
  1439. &committed);
  1440. }
  1441. if (error) {
  1442. ASSERT(committed);
  1443. args->trans = NULL;
  1444. xfs_bmap_cancel(args->flist);
  1445. goto out;
  1446. }
  1447. /*
  1448. * bmap_finish() may have committed the last trans
  1449. * and started a new one. We need the inode to be
  1450. * in all transactions.
  1451. */
  1452. if (committed)
  1453. xfs_trans_ijoin(args->trans, dp);
  1454. } else
  1455. xfs_da_brelse(args->trans, bp);
  1456. }
  1457. error = 0;
  1458. out:
  1459. xfs_da_state_free(state);
  1460. return(error);
  1461. }
  1462. /*
  1463. * Fill in the disk block numbers in the state structure for the buffers
  1464. * that are attached to the state structure.
  1465. * This is done so that we can quickly reattach ourselves to those buffers
  1466. * after some set of transaction commits have released these buffers.
  1467. */
  1468. STATIC int
  1469. xfs_attr_fillstate(xfs_da_state_t *state)
  1470. {
  1471. xfs_da_state_path_t *path;
  1472. xfs_da_state_blk_t *blk;
  1473. int level;
  1474. /*
  1475. * Roll down the "path" in the state structure, storing the on-disk
  1476. * block number for those buffers in the "path".
  1477. */
  1478. path = &state->path;
  1479. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1480. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1481. if (blk->bp) {
  1482. blk->disk_blkno = xfs_da_blkno(blk->bp);
  1483. xfs_da_buf_done(blk->bp);
  1484. blk->bp = NULL;
  1485. } else {
  1486. blk->disk_blkno = 0;
  1487. }
  1488. }
  1489. /*
  1490. * Roll down the "altpath" in the state structure, storing the on-disk
  1491. * block number for those buffers in the "altpath".
  1492. */
  1493. path = &state->altpath;
  1494. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1495. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1496. if (blk->bp) {
  1497. blk->disk_blkno = xfs_da_blkno(blk->bp);
  1498. xfs_da_buf_done(blk->bp);
  1499. blk->bp = NULL;
  1500. } else {
  1501. blk->disk_blkno = 0;
  1502. }
  1503. }
  1504. return(0);
  1505. }
  1506. /*
  1507. * Reattach the buffers to the state structure based on the disk block
  1508. * numbers stored in the state structure.
  1509. * This is done after some set of transaction commits have released those
  1510. * buffers from our grip.
  1511. */
  1512. STATIC int
  1513. xfs_attr_refillstate(xfs_da_state_t *state)
  1514. {
  1515. xfs_da_state_path_t *path;
  1516. xfs_da_state_blk_t *blk;
  1517. int level, error;
  1518. /*
  1519. * Roll down the "path" in the state structure, storing the on-disk
  1520. * block number for those buffers in the "path".
  1521. */
  1522. path = &state->path;
  1523. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1524. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1525. if (blk->disk_blkno) {
  1526. error = xfs_da_read_buf(state->args->trans,
  1527. state->args->dp,
  1528. blk->blkno, blk->disk_blkno,
  1529. &blk->bp, XFS_ATTR_FORK);
  1530. if (error)
  1531. return(error);
  1532. } else {
  1533. blk->bp = NULL;
  1534. }
  1535. }
  1536. /*
  1537. * Roll down the "altpath" in the state structure, storing the on-disk
  1538. * block number for those buffers in the "altpath".
  1539. */
  1540. path = &state->altpath;
  1541. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1542. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1543. if (blk->disk_blkno) {
  1544. error = xfs_da_read_buf(state->args->trans,
  1545. state->args->dp,
  1546. blk->blkno, blk->disk_blkno,
  1547. &blk->bp, XFS_ATTR_FORK);
  1548. if (error)
  1549. return(error);
  1550. } else {
  1551. blk->bp = NULL;
  1552. }
  1553. }
  1554. return(0);
  1555. }
  1556. /*
  1557. * Look up a filename in a node attribute list.
  1558. *
  1559. * This routine gets called for any attribute fork that has more than one
  1560. * block, ie: both true Btree attr lists and for single-leaf-blocks with
  1561. * "remote" values taking up more blocks.
  1562. */
  1563. STATIC int
  1564. xfs_attr_node_get(xfs_da_args_t *args)
  1565. {
  1566. xfs_da_state_t *state;
  1567. xfs_da_state_blk_t *blk;
  1568. int error, retval;
  1569. int i;
  1570. state = xfs_da_state_alloc();
  1571. state->args = args;
  1572. state->mp = args->dp->i_mount;
  1573. state->blocksize = state->mp->m_sb.sb_blocksize;
  1574. state->node_ents = state->mp->m_attr_node_ents;
  1575. /*
  1576. * Search to see if name exists, and get back a pointer to it.
  1577. */
  1578. error = xfs_da_node_lookup_int(state, &retval);
  1579. if (error) {
  1580. retval = error;
  1581. } else if (retval == EEXIST) {
  1582. blk = &state->path.blk[ state->path.active-1 ];
  1583. ASSERT(blk->bp != NULL);
  1584. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1585. /*
  1586. * Get the value, local or "remote"
  1587. */
  1588. retval = xfs_attr_leaf_getvalue(blk->bp, args);
  1589. if (!retval && (args->rmtblkno > 0)
  1590. && !(args->flags & ATTR_KERNOVAL)) {
  1591. retval = xfs_attr_rmtval_get(args);
  1592. }
  1593. }
  1594. /*
  1595. * If not in a transaction, we have to release all the buffers.
  1596. */
  1597. for (i = 0; i < state->path.active; i++) {
  1598. xfs_da_brelse(args->trans, state->path.blk[i].bp);
  1599. state->path.blk[i].bp = NULL;
  1600. }
  1601. xfs_da_state_free(state);
  1602. return(retval);
  1603. }
  1604. STATIC int /* error */
  1605. xfs_attr_node_list(xfs_attr_list_context_t *context)
  1606. {
  1607. attrlist_cursor_kern_t *cursor;
  1608. xfs_attr_leafblock_t *leaf;
  1609. xfs_da_intnode_t *node;
  1610. xfs_da_node_entry_t *btree;
  1611. int error, i;
  1612. xfs_dabuf_t *bp;
  1613. cursor = context->cursor;
  1614. cursor->initted = 1;
  1615. /*
  1616. * Do all sorts of validation on the passed-in cursor structure.
  1617. * If anything is amiss, ignore the cursor and look up the hashval
  1618. * starting from the btree root.
  1619. */
  1620. bp = NULL;
  1621. if (cursor->blkno > 0) {
  1622. error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
  1623. &bp, XFS_ATTR_FORK);
  1624. if ((error != 0) && (error != EFSCORRUPTED))
  1625. return(error);
  1626. if (bp) {
  1627. node = bp->data;
  1628. switch (be16_to_cpu(node->hdr.info.magic)) {
  1629. case XFS_DA_NODE_MAGIC:
  1630. trace_xfs_attr_list_wrong_blk(context);
  1631. xfs_da_brelse(NULL, bp);
  1632. bp = NULL;
  1633. break;
  1634. case XFS_ATTR_LEAF_MAGIC:
  1635. leaf = bp->data;
  1636. if (cursor->hashval > be32_to_cpu(leaf->entries[
  1637. be16_to_cpu(leaf->hdr.count)-1].hashval)) {
  1638. trace_xfs_attr_list_wrong_blk(context);
  1639. xfs_da_brelse(NULL, bp);
  1640. bp = NULL;
  1641. } else if (cursor->hashval <=
  1642. be32_to_cpu(leaf->entries[0].hashval)) {
  1643. trace_xfs_attr_list_wrong_blk(context);
  1644. xfs_da_brelse(NULL, bp);
  1645. bp = NULL;
  1646. }
  1647. break;
  1648. default:
  1649. trace_xfs_attr_list_wrong_blk(context);
  1650. xfs_da_brelse(NULL, bp);
  1651. bp = NULL;
  1652. }
  1653. }
  1654. }
  1655. /*
  1656. * We did not find what we expected given the cursor's contents,
  1657. * so we start from the top and work down based on the hash value.
  1658. * Note that start of node block is same as start of leaf block.
  1659. */
  1660. if (bp == NULL) {
  1661. cursor->blkno = 0;
  1662. for (;;) {
  1663. error = xfs_da_read_buf(NULL, context->dp,
  1664. cursor->blkno, -1, &bp,
  1665. XFS_ATTR_FORK);
  1666. if (error)
  1667. return(error);
  1668. if (unlikely(bp == NULL)) {
  1669. XFS_ERROR_REPORT("xfs_attr_node_list(2)",
  1670. XFS_ERRLEVEL_LOW,
  1671. context->dp->i_mount);
  1672. return(XFS_ERROR(EFSCORRUPTED));
  1673. }
  1674. node = bp->data;
  1675. if (be16_to_cpu(node->hdr.info.magic)
  1676. == XFS_ATTR_LEAF_MAGIC)
  1677. break;
  1678. if (unlikely(be16_to_cpu(node->hdr.info.magic)
  1679. != XFS_DA_NODE_MAGIC)) {
  1680. XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
  1681. XFS_ERRLEVEL_LOW,
  1682. context->dp->i_mount,
  1683. node);
  1684. xfs_da_brelse(NULL, bp);
  1685. return(XFS_ERROR(EFSCORRUPTED));
  1686. }
  1687. btree = node->btree;
  1688. for (i = 0; i < be16_to_cpu(node->hdr.count);
  1689. btree++, i++) {
  1690. if (cursor->hashval
  1691. <= be32_to_cpu(btree->hashval)) {
  1692. cursor->blkno = be32_to_cpu(btree->before);
  1693. trace_xfs_attr_list_node_descend(context,
  1694. btree);
  1695. break;
  1696. }
  1697. }
  1698. if (i == be16_to_cpu(node->hdr.count)) {
  1699. xfs_da_brelse(NULL, bp);
  1700. return(0);
  1701. }
  1702. xfs_da_brelse(NULL, bp);
  1703. }
  1704. }
  1705. ASSERT(bp != NULL);
  1706. /*
  1707. * Roll upward through the blocks, processing each leaf block in
  1708. * order. As long as there is space in the result buffer, keep
  1709. * adding the information.
  1710. */
  1711. for (;;) {
  1712. leaf = bp->data;
  1713. if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
  1714. != XFS_ATTR_LEAF_MAGIC)) {
  1715. XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
  1716. XFS_ERRLEVEL_LOW,
  1717. context->dp->i_mount, leaf);
  1718. xfs_da_brelse(NULL, bp);
  1719. return(XFS_ERROR(EFSCORRUPTED));
  1720. }
  1721. error = xfs_attr_leaf_list_int(bp, context);
  1722. if (error) {
  1723. xfs_da_brelse(NULL, bp);
  1724. return error;
  1725. }
  1726. if (context->seen_enough || leaf->hdr.info.forw == 0)
  1727. break;
  1728. cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
  1729. xfs_da_brelse(NULL, bp);
  1730. error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
  1731. &bp, XFS_ATTR_FORK);
  1732. if (error)
  1733. return(error);
  1734. if (unlikely((bp == NULL))) {
  1735. XFS_ERROR_REPORT("xfs_attr_node_list(5)",
  1736. XFS_ERRLEVEL_LOW,
  1737. context->dp->i_mount);
  1738. return(XFS_ERROR(EFSCORRUPTED));
  1739. }
  1740. }
  1741. xfs_da_brelse(NULL, bp);
  1742. return(0);
  1743. }
  1744. /*========================================================================
  1745. * External routines for manipulating out-of-line attribute values.
  1746. *========================================================================*/
  1747. /*
  1748. * Read the value associated with an attribute from the out-of-line buffer
  1749. * that we stored it in.
  1750. */
  1751. int
  1752. xfs_attr_rmtval_get(xfs_da_args_t *args)
  1753. {
  1754. xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
  1755. xfs_mount_t *mp;
  1756. xfs_daddr_t dblkno;
  1757. void *dst;
  1758. xfs_buf_t *bp;
  1759. int nmap, error, tmp, valuelen, blkcnt, i;
  1760. xfs_dablk_t lblkno;
  1761. ASSERT(!(args->flags & ATTR_KERNOVAL));
  1762. mp = args->dp->i_mount;
  1763. dst = args->value;
  1764. valuelen = args->valuelen;
  1765. lblkno = args->rmtblkno;
  1766. while (valuelen > 0) {
  1767. nmap = ATTR_RMTVALUE_MAPSIZE;
  1768. error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
  1769. args->rmtblkcnt,
  1770. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1771. NULL, 0, map, &nmap, NULL);
  1772. if (error)
  1773. return(error);
  1774. ASSERT(nmap >= 1);
  1775. for (i = 0; (i < nmap) && (valuelen > 0); i++) {
  1776. ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
  1777. (map[i].br_startblock != HOLESTARTBLOCK));
  1778. dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
  1779. blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  1780. error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
  1781. blkcnt, XBF_LOCK | XBF_DONT_BLOCK,
  1782. &bp);
  1783. if (error)
  1784. return(error);
  1785. tmp = (valuelen < XFS_BUF_SIZE(bp))
  1786. ? valuelen : XFS_BUF_SIZE(bp);
  1787. xfs_biomove(bp, 0, tmp, dst, XBF_READ);
  1788. xfs_buf_relse(bp);
  1789. dst += tmp;
  1790. valuelen -= tmp;
  1791. lblkno += map[i].br_blockcount;
  1792. }
  1793. }
  1794. ASSERT(valuelen == 0);
  1795. return(0);
  1796. }
  1797. /*
  1798. * Write the value associated with an attribute into the out-of-line buffer
  1799. * that we have defined for it.
  1800. */
  1801. STATIC int
  1802. xfs_attr_rmtval_set(xfs_da_args_t *args)
  1803. {
  1804. xfs_mount_t *mp;
  1805. xfs_fileoff_t lfileoff;
  1806. xfs_inode_t *dp;
  1807. xfs_bmbt_irec_t map;
  1808. xfs_daddr_t dblkno;
  1809. void *src;
  1810. xfs_buf_t *bp;
  1811. xfs_dablk_t lblkno;
  1812. int blkcnt, valuelen, nmap, error, tmp, committed;
  1813. dp = args->dp;
  1814. mp = dp->i_mount;
  1815. src = args->value;
  1816. /*
  1817. * Find a "hole" in the attribute address space large enough for
  1818. * us to drop the new attribute's value into.
  1819. */
  1820. blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
  1821. lfileoff = 0;
  1822. error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
  1823. XFS_ATTR_FORK);
  1824. if (error) {
  1825. return(error);
  1826. }
  1827. args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
  1828. args->rmtblkcnt = blkcnt;
  1829. /*
  1830. * Roll through the "value", allocating blocks on disk as required.
  1831. */
  1832. while (blkcnt > 0) {
  1833. /*
  1834. * Allocate a single extent, up to the size of the value.
  1835. */
  1836. xfs_bmap_init(args->flist, args->firstblock);
  1837. nmap = 1;
  1838. error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
  1839. blkcnt,
  1840. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
  1841. XFS_BMAPI_WRITE,
  1842. args->firstblock, args->total, &map, &nmap,
  1843. args->flist);
  1844. if (!error) {
  1845. error = xfs_bmap_finish(&args->trans, args->flist,
  1846. &committed);
  1847. }
  1848. if (error) {
  1849. ASSERT(committed);
  1850. args->trans = NULL;
  1851. xfs_bmap_cancel(args->flist);
  1852. return(error);
  1853. }
  1854. /*
  1855. * bmap_finish() may have committed the last trans and started
  1856. * a new one. We need the inode to be in all transactions.
  1857. */
  1858. if (committed)
  1859. xfs_trans_ijoin(args->trans, dp);
  1860. ASSERT(nmap == 1);
  1861. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  1862. (map.br_startblock != HOLESTARTBLOCK));
  1863. lblkno += map.br_blockcount;
  1864. blkcnt -= map.br_blockcount;
  1865. /*
  1866. * Start the next trans in the chain.
  1867. */
  1868. error = xfs_trans_roll(&args->trans, dp);
  1869. if (error)
  1870. return (error);
  1871. }
  1872. /*
  1873. * Roll through the "value", copying the attribute value to the
  1874. * already-allocated blocks. Blocks are written synchronously
  1875. * so that we can know they are all on disk before we turn off
  1876. * the INCOMPLETE flag.
  1877. */
  1878. lblkno = args->rmtblkno;
  1879. valuelen = args->valuelen;
  1880. while (valuelen > 0) {
  1881. /*
  1882. * Try to remember where we decided to put the value.
  1883. */
  1884. xfs_bmap_init(args->flist, args->firstblock);
  1885. nmap = 1;
  1886. error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
  1887. args->rmtblkcnt,
  1888. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1889. args->firstblock, 0, &map, &nmap,
  1890. NULL);
  1891. if (error) {
  1892. return(error);
  1893. }
  1894. ASSERT(nmap == 1);
  1895. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  1896. (map.br_startblock != HOLESTARTBLOCK));
  1897. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  1898. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  1899. bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt,
  1900. XBF_LOCK | XBF_DONT_BLOCK);
  1901. ASSERT(bp);
  1902. ASSERT(!XFS_BUF_GETERROR(bp));
  1903. tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
  1904. XFS_BUF_SIZE(bp);
  1905. xfs_biomove(bp, 0, tmp, src, XBF_WRITE);
  1906. if (tmp < XFS_BUF_SIZE(bp))
  1907. xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
  1908. if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
  1909. return (error);
  1910. }
  1911. src += tmp;
  1912. valuelen -= tmp;
  1913. lblkno += map.br_blockcount;
  1914. }
  1915. ASSERT(valuelen == 0);
  1916. return(0);
  1917. }
  1918. /*
  1919. * Remove the value associated with an attribute by deleting the
  1920. * out-of-line buffer that it is stored on.
  1921. */
  1922. STATIC int
  1923. xfs_attr_rmtval_remove(xfs_da_args_t *args)
  1924. {
  1925. xfs_mount_t *mp;
  1926. xfs_bmbt_irec_t map;
  1927. xfs_buf_t *bp;
  1928. xfs_daddr_t dblkno;
  1929. xfs_dablk_t lblkno;
  1930. int valuelen, blkcnt, nmap, error, done, committed;
  1931. mp = args->dp->i_mount;
  1932. /*
  1933. * Roll through the "value", invalidating the attribute value's
  1934. * blocks.
  1935. */
  1936. lblkno = args->rmtblkno;
  1937. valuelen = args->rmtblkcnt;
  1938. while (valuelen > 0) {
  1939. /*
  1940. * Try to remember where we decided to put the value.
  1941. */
  1942. xfs_bmap_init(args->flist, args->firstblock);
  1943. nmap = 1;
  1944. error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
  1945. args->rmtblkcnt,
  1946. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1947. args->firstblock, 0, &map, &nmap,
  1948. args->flist);
  1949. if (error) {
  1950. return(error);
  1951. }
  1952. ASSERT(nmap == 1);
  1953. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  1954. (map.br_startblock != HOLESTARTBLOCK));
  1955. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  1956. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  1957. /*
  1958. * If the "remote" value is in the cache, remove it.
  1959. */
  1960. bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
  1961. if (bp) {
  1962. XFS_BUF_STALE(bp);
  1963. XFS_BUF_UNDELAYWRITE(bp);
  1964. xfs_buf_relse(bp);
  1965. bp = NULL;
  1966. }
  1967. valuelen -= map.br_blockcount;
  1968. lblkno += map.br_blockcount;
  1969. }
  1970. /*
  1971. * Keep de-allocating extents until the remote-value region is gone.
  1972. */
  1973. lblkno = args->rmtblkno;
  1974. blkcnt = args->rmtblkcnt;
  1975. done = 0;
  1976. while (!done) {
  1977. xfs_bmap_init(args->flist, args->firstblock);
  1978. error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
  1979. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1980. 1, args->firstblock, args->flist,
  1981. &done);
  1982. if (!error) {
  1983. error = xfs_bmap_finish(&args->trans, args->flist,
  1984. &committed);
  1985. }
  1986. if (error) {
  1987. ASSERT(committed);
  1988. args->trans = NULL;
  1989. xfs_bmap_cancel(args->flist);
  1990. return(error);
  1991. }
  1992. /*
  1993. * bmap_finish() may have committed the last trans and started
  1994. * a new one. We need the inode to be in all transactions.
  1995. */
  1996. if (committed)
  1997. xfs_trans_ijoin(args->trans, args->dp);
  1998. /*
  1999. * Close out trans and start the next one in the chain.
  2000. */
  2001. error = xfs_trans_roll(&args->trans, args->dp);
  2002. if (error)
  2003. return (error);
  2004. }
  2005. return(0);
  2006. }