xfs_attr.c 55 KB

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