xfs_attr.c 67 KB

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