xfs_attr.c 68 KB

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