xfs_attr.c 67 KB

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