xfs_attr.c 59 KB

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