xfs_attr.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638
  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);
  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);
  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, NULL);
  1836. if (error) {
  1837. return(error);
  1838. }
  1839. ASSERT(nmap == 1);
  1840. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  1841. (map.br_startblock != HOLESTARTBLOCK));
  1842. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  1843. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  1844. bp = xfs_buf_get_flags(mp->m_ddev_targp, dblkno,
  1845. blkcnt, XFS_BUF_LOCK);
  1846. ASSERT(bp);
  1847. ASSERT(!XFS_BUF_GETERROR(bp));
  1848. tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
  1849. XFS_BUF_SIZE(bp);
  1850. xfs_biomove(bp, 0, tmp, src, XFS_B_WRITE);
  1851. if (tmp < XFS_BUF_SIZE(bp))
  1852. xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
  1853. if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
  1854. return (error);
  1855. }
  1856. src += tmp;
  1857. valuelen -= tmp;
  1858. lblkno += map.br_blockcount;
  1859. }
  1860. ASSERT(valuelen == 0);
  1861. return(0);
  1862. }
  1863. /*
  1864. * Remove the value associated with an attribute by deleting the
  1865. * out-of-line buffer that it is stored on.
  1866. */
  1867. STATIC int
  1868. xfs_attr_rmtval_remove(xfs_da_args_t *args)
  1869. {
  1870. xfs_mount_t *mp;
  1871. xfs_bmbt_irec_t map;
  1872. xfs_buf_t *bp;
  1873. xfs_daddr_t dblkno;
  1874. xfs_dablk_t lblkno;
  1875. int valuelen, blkcnt, nmap, error, done, committed;
  1876. mp = args->dp->i_mount;
  1877. /*
  1878. * Roll through the "value", invalidating the attribute value's
  1879. * blocks.
  1880. */
  1881. lblkno = args->rmtblkno;
  1882. valuelen = args->rmtblkcnt;
  1883. while (valuelen > 0) {
  1884. /*
  1885. * Try to remember where we decided to put the value.
  1886. */
  1887. XFS_BMAP_INIT(args->flist, args->firstblock);
  1888. nmap = 1;
  1889. error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
  1890. args->rmtblkcnt,
  1891. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1892. args->firstblock, 0, &map, &nmap,
  1893. args->flist);
  1894. if (error) {
  1895. return(error);
  1896. }
  1897. ASSERT(nmap == 1);
  1898. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  1899. (map.br_startblock != HOLESTARTBLOCK));
  1900. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  1901. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  1902. /*
  1903. * If the "remote" value is in the cache, remove it.
  1904. */
  1905. bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt,
  1906. XFS_INCORE_TRYLOCK);
  1907. if (bp) {
  1908. XFS_BUF_STALE(bp);
  1909. XFS_BUF_UNDELAYWRITE(bp);
  1910. xfs_buf_relse(bp);
  1911. bp = NULL;
  1912. }
  1913. valuelen -= map.br_blockcount;
  1914. lblkno += map.br_blockcount;
  1915. }
  1916. /*
  1917. * Keep de-allocating extents until the remote-value region is gone.
  1918. */
  1919. lblkno = args->rmtblkno;
  1920. blkcnt = args->rmtblkcnt;
  1921. done = 0;
  1922. while (!done) {
  1923. XFS_BMAP_INIT(args->flist, args->firstblock);
  1924. error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
  1925. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1926. 1, args->firstblock, args->flist, &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. 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. struct vnode *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. 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. vnode_t *vp)
  2115. {
  2116. return xfs_acl_vhasacl_access(vp);
  2117. }
  2118. STATIC int
  2119. posix_acl_default_set(
  2120. 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. 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. struct vnode *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. 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. struct vnode *vp, char *name, void *data, size_t size, int xflags)
  2166. {
  2167. int error;
  2168. VOP_ATTR_SET(vp, name, data, size, xflags, NULL, error);
  2169. return -error;
  2170. }
  2171. STATIC int
  2172. attr_generic_get(
  2173. struct vnode *vp, char *name, void *data, size_t size, int xflags)
  2174. {
  2175. int error, asize = size;
  2176. VOP_ATTR_GET(vp, name, data, &asize, xflags, NULL, error);
  2177. if (!error)
  2178. return asize;
  2179. return -error;
  2180. }
  2181. STATIC int
  2182. attr_generic_remove(
  2183. struct vnode *vp, char *name, int xflags)
  2184. {
  2185. int error;
  2186. VOP_ATTR_REMOVE(vp, name, xflags, NULL, error);
  2187. return -error;
  2188. }
  2189. STATIC int
  2190. attr_generic_listadd(
  2191. attrnames_t *prefix,
  2192. attrnames_t *namesp,
  2193. void *data,
  2194. size_t size,
  2195. ssize_t *result)
  2196. {
  2197. char *p = data + *result;
  2198. *result += prefix->attr_namelen;
  2199. *result += namesp->attr_namelen + 1;
  2200. if (!size)
  2201. return 0;
  2202. if (*result > size)
  2203. return -ERANGE;
  2204. strcpy(p, prefix->attr_name);
  2205. p += prefix->attr_namelen;
  2206. strcpy(p, namesp->attr_name);
  2207. p += namesp->attr_namelen + 1;
  2208. return 0;
  2209. }
  2210. STATIC int
  2211. attr_system_list(
  2212. struct vnode *vp,
  2213. void *data,
  2214. size_t size,
  2215. ssize_t *result)
  2216. {
  2217. attrnames_t *namesp;
  2218. int i, error = 0;
  2219. for (i = 0; i < ATTR_SYSCOUNT; i++) {
  2220. namesp = attr_system_names[i];
  2221. if (!namesp->attr_exists || !namesp->attr_exists(vp))
  2222. continue;
  2223. error = attr_generic_listadd(&attr_system, namesp,
  2224. data, size, result);
  2225. if (error)
  2226. break;
  2227. }
  2228. return error;
  2229. }
  2230. int
  2231. attr_generic_list(
  2232. struct vnode *vp, void *data, size_t size, int xflags, ssize_t *result)
  2233. {
  2234. attrlist_cursor_kern_t cursor = { 0 };
  2235. int error;
  2236. VOP_ATTR_LIST(vp, data, size, xflags, &cursor, NULL, error);
  2237. if (error > 0)
  2238. return -error;
  2239. *result = -error;
  2240. return attr_system_list(vp, data, size, result);
  2241. }
  2242. attrnames_t *
  2243. attr_lookup_namespace(
  2244. char *name,
  2245. struct attrnames **names,
  2246. int nnames)
  2247. {
  2248. int i;
  2249. for (i = 0; i < nnames; i++)
  2250. if (!strncmp(name, names[i]->attr_name, names[i]->attr_namelen))
  2251. return names[i];
  2252. return NULL;
  2253. }
  2254. /*
  2255. * Some checks to prevent people abusing EAs to get over quota:
  2256. * - Don't allow modifying user EAs on devices/symlinks;
  2257. * - Don't allow modifying user EAs if sticky bit set;
  2258. */
  2259. STATIC int
  2260. attr_user_capable(
  2261. struct vnode *vp,
  2262. cred_t *cred)
  2263. {
  2264. struct inode *inode = vn_to_inode(vp);
  2265. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  2266. return -EPERM;
  2267. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode) &&
  2268. !capable(CAP_SYS_ADMIN))
  2269. return -EPERM;
  2270. if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
  2271. (current_fsuid(cred) != inode->i_uid) && !capable(CAP_FOWNER))
  2272. return -EPERM;
  2273. return 0;
  2274. }
  2275. STATIC int
  2276. attr_trusted_capable(
  2277. struct vnode *vp,
  2278. cred_t *cred)
  2279. {
  2280. struct inode *inode = vn_to_inode(vp);
  2281. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  2282. return -EPERM;
  2283. if (!capable(CAP_SYS_ADMIN))
  2284. return -EPERM;
  2285. return 0;
  2286. }
  2287. STATIC int
  2288. attr_secure_capable(
  2289. struct vnode *vp,
  2290. cred_t *cred)
  2291. {
  2292. return -ENOSECURITY;
  2293. }
  2294. STATIC int
  2295. attr_system_set(
  2296. struct vnode *vp, char *name, void *data, size_t size, int xflags)
  2297. {
  2298. attrnames_t *namesp;
  2299. int error;
  2300. if (xflags & ATTR_CREATE)
  2301. return -EINVAL;
  2302. namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
  2303. if (!namesp)
  2304. return -EOPNOTSUPP;
  2305. error = namesp->attr_set(vp, name, data, size, xflags);
  2306. if (!error)
  2307. error = vn_revalidate(vp);
  2308. return error;
  2309. }
  2310. STATIC int
  2311. attr_system_get(
  2312. struct vnode *vp, char *name, void *data, size_t size, int xflags)
  2313. {
  2314. attrnames_t *namesp;
  2315. namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
  2316. if (!namesp)
  2317. return -EOPNOTSUPP;
  2318. return namesp->attr_get(vp, name, data, size, xflags);
  2319. }
  2320. STATIC int
  2321. attr_system_remove(
  2322. struct vnode *vp, char *name, int xflags)
  2323. {
  2324. attrnames_t *namesp;
  2325. namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
  2326. if (!namesp)
  2327. return -EOPNOTSUPP;
  2328. return namesp->attr_remove(vp, name, xflags);
  2329. }
  2330. struct attrnames attr_system = {
  2331. .attr_name = "system.",
  2332. .attr_namelen = sizeof("system.") - 1,
  2333. .attr_flag = ATTR_SYSTEM,
  2334. .attr_get = attr_system_get,
  2335. .attr_set = attr_system_set,
  2336. .attr_remove = attr_system_remove,
  2337. .attr_capable = (attrcapable_t)fs_noerr,
  2338. };
  2339. struct attrnames attr_trusted = {
  2340. .attr_name = "trusted.",
  2341. .attr_namelen = sizeof("trusted.") - 1,
  2342. .attr_flag = ATTR_ROOT,
  2343. .attr_get = attr_generic_get,
  2344. .attr_set = attr_generic_set,
  2345. .attr_remove = attr_generic_remove,
  2346. .attr_capable = attr_trusted_capable,
  2347. };
  2348. struct attrnames attr_secure = {
  2349. .attr_name = "security.",
  2350. .attr_namelen = sizeof("security.") - 1,
  2351. .attr_flag = ATTR_SECURE,
  2352. .attr_get = attr_generic_get,
  2353. .attr_set = attr_generic_set,
  2354. .attr_remove = attr_generic_remove,
  2355. .attr_capable = attr_secure_capable,
  2356. };
  2357. struct attrnames attr_user = {
  2358. .attr_name = "user.",
  2359. .attr_namelen = sizeof("user.") - 1,
  2360. .attr_get = attr_generic_get,
  2361. .attr_set = attr_generic_set,
  2362. .attr_remove = attr_generic_remove,
  2363. .attr_capable = attr_user_capable,
  2364. };
  2365. struct attrnames *attr_namespaces[] =
  2366. { &attr_system, &attr_trusted, &attr_secure, &attr_user };