xfs_attr.c 61 KB

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