xfs_attr.c 69 KB

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