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