xfs_attr.c 68 KB

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