xfs_attr.c 67 KB

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