xattr.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /*
  2. * Copyright (C) International Business Machines Corp., 2000-2004
  3. * Copyright (C) Christoph Hellwig, 2002
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/fs.h>
  20. #include <linux/xattr.h>
  21. #include <linux/posix_acl_xattr.h>
  22. #include <linux/quotaops.h>
  23. #include <linux/security.h>
  24. #include "jfs_incore.h"
  25. #include "jfs_superblock.h"
  26. #include "jfs_dmap.h"
  27. #include "jfs_debug.h"
  28. #include "jfs_dinode.h"
  29. #include "jfs_extent.h"
  30. #include "jfs_metapage.h"
  31. #include "jfs_xattr.h"
  32. #include "jfs_acl.h"
  33. /*
  34. * jfs_xattr.c: extended attribute service
  35. *
  36. * Overall design --
  37. *
  38. * Format:
  39. *
  40. * Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
  41. * value) and a variable (0 or more) number of extended attribute
  42. * entries. Each extended attribute entry (jfs_ea) is a <name,value> double
  43. * where <name> is constructed from a null-terminated ascii string
  44. * (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
  45. * (1 ... 65535 bytes). The in-memory format is
  46. *
  47. * 0 1 2 4 4 + namelen + 1
  48. * +-------+--------+--------+----------------+-------------------+
  49. * | Flags | Name | Value | Name String \0 | Data . . . . |
  50. * | | Length | Length | | |
  51. * +-------+--------+--------+----------------+-------------------+
  52. *
  53. * A jfs_ea_list then is structured as
  54. *
  55. * 0 4 4 + EA_SIZE(ea1)
  56. * +------------+-------------------+--------------------+-----
  57. * | Overall EA | First FEA Element | Second FEA Element | .....
  58. * | List Size | | |
  59. * +------------+-------------------+--------------------+-----
  60. *
  61. * On-disk:
  62. *
  63. * FEALISTs are stored on disk using blocks allocated by dbAlloc() and
  64. * written directly. An EA list may be in-lined in the inode if there is
  65. * sufficient room available.
  66. */
  67. struct ea_buffer {
  68. int flag; /* Indicates what storage xattr points to */
  69. int max_size; /* largest xattr that fits in current buffer */
  70. dxd_t new_ea; /* dxd to replace ea when modifying xattr */
  71. struct metapage *mp; /* metapage containing ea list */
  72. struct jfs_ea_list *xattr; /* buffer containing ea list */
  73. };
  74. /*
  75. * ea_buffer.flag values
  76. */
  77. #define EA_INLINE 0x0001
  78. #define EA_EXTENT 0x0002
  79. #define EA_NEW 0x0004
  80. #define EA_MALLOC 0x0008
  81. /* Namespaces */
  82. #define XATTR_SYSTEM_PREFIX "system."
  83. #define XATTR_SYSTEM_PREFIX_LEN (sizeof (XATTR_SYSTEM_PREFIX) - 1)
  84. #define XATTR_USER_PREFIX "user."
  85. #define XATTR_USER_PREFIX_LEN (sizeof (XATTR_USER_PREFIX) - 1)
  86. #define XATTR_OS2_PREFIX "os2."
  87. #define XATTR_OS2_PREFIX_LEN (sizeof (XATTR_OS2_PREFIX) - 1)
  88. /* XATTR_SECURITY_PREFIX is defined in include/linux/xattr.h */
  89. #define XATTR_SECURITY_PREFIX_LEN (sizeof (XATTR_SECURITY_PREFIX) - 1)
  90. #define XATTR_TRUSTED_PREFIX "trusted."
  91. #define XATTR_TRUSTED_PREFIX_LEN (sizeof (XATTR_TRUSTED_PREFIX) - 1)
  92. /*
  93. * These three routines are used to recognize on-disk extended attributes
  94. * that are in a recognized namespace. If the attribute is not recognized,
  95. * "os2." is prepended to the name
  96. */
  97. static inline int is_os2_xattr(struct jfs_ea *ea)
  98. {
  99. /*
  100. * Check for "system."
  101. */
  102. if ((ea->namelen >= XATTR_SYSTEM_PREFIX_LEN) &&
  103. !strncmp(ea->name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  104. return FALSE;
  105. /*
  106. * Check for "user."
  107. */
  108. if ((ea->namelen >= XATTR_USER_PREFIX_LEN) &&
  109. !strncmp(ea->name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
  110. return FALSE;
  111. /*
  112. * Check for "security."
  113. */
  114. if ((ea->namelen >= XATTR_SECURITY_PREFIX_LEN) &&
  115. !strncmp(ea->name, XATTR_SECURITY_PREFIX,
  116. XATTR_SECURITY_PREFIX_LEN))
  117. return FALSE;
  118. /*
  119. * Check for "trusted."
  120. */
  121. if ((ea->namelen >= XATTR_TRUSTED_PREFIX_LEN) &&
  122. !strncmp(ea->name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
  123. return FALSE;
  124. /*
  125. * Add any other valid namespace prefixes here
  126. */
  127. /*
  128. * We assume it's OS/2's flat namespace
  129. */
  130. return TRUE;
  131. }
  132. static inline int name_size(struct jfs_ea *ea)
  133. {
  134. if (is_os2_xattr(ea))
  135. return ea->namelen + XATTR_OS2_PREFIX_LEN;
  136. else
  137. return ea->namelen;
  138. }
  139. static inline int copy_name(char *buffer, struct jfs_ea *ea)
  140. {
  141. int len = ea->namelen;
  142. if (is_os2_xattr(ea)) {
  143. memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
  144. buffer += XATTR_OS2_PREFIX_LEN;
  145. len += XATTR_OS2_PREFIX_LEN;
  146. }
  147. memcpy(buffer, ea->name, ea->namelen);
  148. buffer[ea->namelen] = 0;
  149. return len;
  150. }
  151. /* Forward references */
  152. static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
  153. /*
  154. * NAME: ea_write_inline
  155. *
  156. * FUNCTION: Attempt to write an EA inline if area is available
  157. *
  158. * PRE CONDITIONS:
  159. * Already verified that the specified EA is small enough to fit inline
  160. *
  161. * PARAMETERS:
  162. * ip - Inode pointer
  163. * ealist - EA list pointer
  164. * size - size of ealist in bytes
  165. * ea - dxd_t structure to be filled in with necessary EA information
  166. * if we successfully copy the EA inline
  167. *
  168. * NOTES:
  169. * Checks if the inode's inline area is available. If so, copies EA inline
  170. * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
  171. * have to be put into an extent.
  172. *
  173. * RETURNS: 0 for successful copy to inline area; -1 if area not available
  174. */
  175. static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
  176. int size, dxd_t * ea)
  177. {
  178. struct jfs_inode_info *ji = JFS_IP(ip);
  179. /*
  180. * Make sure we have an EA -- the NULL EA list is valid, but you
  181. * can't copy it!
  182. */
  183. if (ealist && size > sizeof (struct jfs_ea_list)) {
  184. assert(size <= sizeof (ji->i_inline_ea));
  185. /*
  186. * See if the space is available or if it is already being
  187. * used for an inline EA.
  188. */
  189. if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
  190. return -EPERM;
  191. DXDsize(ea, size);
  192. DXDlength(ea, 0);
  193. DXDaddress(ea, 0);
  194. memcpy(ji->i_inline_ea, ealist, size);
  195. ea->flag = DXD_INLINE;
  196. ji->mode2 &= ~INLINEEA;
  197. } else {
  198. ea->flag = 0;
  199. DXDsize(ea, 0);
  200. DXDlength(ea, 0);
  201. DXDaddress(ea, 0);
  202. /* Free up INLINE area */
  203. if (ji->ea.flag & DXD_INLINE)
  204. ji->mode2 |= INLINEEA;
  205. }
  206. return 0;
  207. }
  208. /*
  209. * NAME: ea_write
  210. *
  211. * FUNCTION: Write an EA for an inode
  212. *
  213. * PRE CONDITIONS: EA has been verified
  214. *
  215. * PARAMETERS:
  216. * ip - Inode pointer
  217. * ealist - EA list pointer
  218. * size - size of ealist in bytes
  219. * ea - dxd_t structure to be filled in appropriately with where the
  220. * EA was copied
  221. *
  222. * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
  223. * extent and synchronously writes it to those blocks.
  224. *
  225. * RETURNS: 0 for success; Anything else indicates failure
  226. */
  227. static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
  228. dxd_t * ea)
  229. {
  230. struct super_block *sb = ip->i_sb;
  231. struct jfs_inode_info *ji = JFS_IP(ip);
  232. struct jfs_sb_info *sbi = JFS_SBI(sb);
  233. int nblocks;
  234. s64 blkno;
  235. int rc = 0, i;
  236. char *cp;
  237. s32 nbytes, nb;
  238. s32 bytes_to_write;
  239. struct metapage *mp;
  240. /*
  241. * Quick check to see if this is an in-linable EA. Short EAs
  242. * and empty EAs are all in-linable, provided the space exists.
  243. */
  244. if (!ealist || size <= sizeof (ji->i_inline_ea)) {
  245. if (!ea_write_inline(ip, ealist, size, ea))
  246. return 0;
  247. }
  248. /* figure out how many blocks we need */
  249. nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
  250. /* Allocate new blocks to quota. */
  251. if (DQUOT_ALLOC_BLOCK(ip, nblocks)) {
  252. return -EDQUOT;
  253. }
  254. rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
  255. if (rc) {
  256. /*Rollback quota allocation. */
  257. DQUOT_FREE_BLOCK(ip, nblocks);
  258. return rc;
  259. }
  260. /*
  261. * Now have nblocks worth of storage to stuff into the FEALIST.
  262. * loop over the FEALIST copying data into the buffer one page at
  263. * a time.
  264. */
  265. cp = (char *) ealist;
  266. nbytes = size;
  267. for (i = 0; i < nblocks; i += sbi->nbperpage) {
  268. /*
  269. * Determine how many bytes for this request, and round up to
  270. * the nearest aggregate block size
  271. */
  272. nb = min(PSIZE, nbytes);
  273. bytes_to_write =
  274. ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
  275. << sb->s_blocksize_bits;
  276. if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
  277. rc = -EIO;
  278. goto failed;
  279. }
  280. memcpy(mp->data, cp, nb);
  281. /*
  282. * We really need a way to propagate errors for
  283. * forced writes like this one. --hch
  284. *
  285. * (__write_metapage => release_metapage => flush_metapage)
  286. */
  287. #ifdef _JFS_FIXME
  288. if ((rc = flush_metapage(mp))) {
  289. /*
  290. * the write failed -- this means that the buffer
  291. * is still assigned and the blocks are not being
  292. * used. this seems like the best error recovery
  293. * we can get ...
  294. */
  295. goto failed;
  296. }
  297. #else
  298. flush_metapage(mp);
  299. #endif
  300. cp += PSIZE;
  301. nbytes -= nb;
  302. }
  303. ea->flag = DXD_EXTENT;
  304. DXDsize(ea, le32_to_cpu(ealist->size));
  305. DXDlength(ea, nblocks);
  306. DXDaddress(ea, blkno);
  307. /* Free up INLINE area */
  308. if (ji->ea.flag & DXD_INLINE)
  309. ji->mode2 |= INLINEEA;
  310. return 0;
  311. failed:
  312. /* Rollback quota allocation. */
  313. DQUOT_FREE_BLOCK(ip, nblocks);
  314. dbFree(ip, blkno, nblocks);
  315. return rc;
  316. }
  317. /*
  318. * NAME: ea_read_inline
  319. *
  320. * FUNCTION: Read an inlined EA into user's buffer
  321. *
  322. * PARAMETERS:
  323. * ip - Inode pointer
  324. * ealist - Pointer to buffer to fill in with EA
  325. *
  326. * RETURNS: 0
  327. */
  328. static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
  329. {
  330. struct jfs_inode_info *ji = JFS_IP(ip);
  331. int ea_size = sizeDXD(&ji->ea);
  332. if (ea_size == 0) {
  333. ealist->size = 0;
  334. return 0;
  335. }
  336. /* Sanity Check */
  337. if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
  338. return -EIO;
  339. if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
  340. != ea_size)
  341. return -EIO;
  342. memcpy(ealist, ji->i_inline_ea, ea_size);
  343. return 0;
  344. }
  345. /*
  346. * NAME: ea_read
  347. *
  348. * FUNCTION: copy EA data into user's buffer
  349. *
  350. * PARAMETERS:
  351. * ip - Inode pointer
  352. * ealist - Pointer to buffer to fill in with EA
  353. *
  354. * NOTES: If EA is inline calls ea_read_inline() to copy EA.
  355. *
  356. * RETURNS: 0 for success; other indicates failure
  357. */
  358. static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
  359. {
  360. struct super_block *sb = ip->i_sb;
  361. struct jfs_inode_info *ji = JFS_IP(ip);
  362. struct jfs_sb_info *sbi = JFS_SBI(sb);
  363. int nblocks;
  364. s64 blkno;
  365. char *cp = (char *) ealist;
  366. int i;
  367. int nbytes, nb;
  368. s32 bytes_to_read;
  369. struct metapage *mp;
  370. /* quick check for in-line EA */
  371. if (ji->ea.flag & DXD_INLINE)
  372. return ea_read_inline(ip, ealist);
  373. nbytes = sizeDXD(&ji->ea);
  374. if (!nbytes) {
  375. jfs_error(sb, "ea_read: nbytes is 0");
  376. return -EIO;
  377. }
  378. /*
  379. * Figure out how many blocks were allocated when this EA list was
  380. * originally written to disk.
  381. */
  382. nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
  383. blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
  384. /*
  385. * I have found the disk blocks which were originally used to store
  386. * the FEALIST. now i loop over each contiguous block copying the
  387. * data into the buffer.
  388. */
  389. for (i = 0; i < nblocks; i += sbi->nbperpage) {
  390. /*
  391. * Determine how many bytes for this request, and round up to
  392. * the nearest aggregate block size
  393. */
  394. nb = min(PSIZE, nbytes);
  395. bytes_to_read =
  396. ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
  397. << sb->s_blocksize_bits;
  398. if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
  399. return -EIO;
  400. memcpy(cp, mp->data, nb);
  401. release_metapage(mp);
  402. cp += PSIZE;
  403. nbytes -= nb;
  404. }
  405. return 0;
  406. }
  407. /*
  408. * NAME: ea_get
  409. *
  410. * FUNCTION: Returns buffer containing existing extended attributes.
  411. * The size of the buffer will be the larger of the existing
  412. * attributes size, or min_size.
  413. *
  414. * The buffer, which may be inlined in the inode or in the
  415. * page cache must be release by calling ea_release or ea_put
  416. *
  417. * PARAMETERS:
  418. * inode - Inode pointer
  419. * ea_buf - Structure to be populated with ealist and its metadata
  420. * min_size- minimum size of buffer to be returned
  421. *
  422. * RETURNS: 0 for success; Other indicates failure
  423. */
  424. static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
  425. {
  426. struct jfs_inode_info *ji = JFS_IP(inode);
  427. struct super_block *sb = inode->i_sb;
  428. int size;
  429. int ea_size = sizeDXD(&ji->ea);
  430. int blocks_needed, current_blocks;
  431. s64 blkno;
  432. int rc;
  433. int quota_allocation = 0;
  434. /* When fsck.jfs clears a bad ea, it doesn't clear the size */
  435. if (ji->ea.flag == 0)
  436. ea_size = 0;
  437. if (ea_size == 0) {
  438. if (min_size == 0) {
  439. ea_buf->flag = 0;
  440. ea_buf->max_size = 0;
  441. ea_buf->xattr = NULL;
  442. return 0;
  443. }
  444. if ((min_size <= sizeof (ji->i_inline_ea)) &&
  445. (ji->mode2 & INLINEEA)) {
  446. ea_buf->flag = EA_INLINE | EA_NEW;
  447. ea_buf->max_size = sizeof (ji->i_inline_ea);
  448. ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
  449. DXDlength(&ea_buf->new_ea, 0);
  450. DXDaddress(&ea_buf->new_ea, 0);
  451. ea_buf->new_ea.flag = DXD_INLINE;
  452. DXDsize(&ea_buf->new_ea, min_size);
  453. return 0;
  454. }
  455. current_blocks = 0;
  456. } else if (ji->ea.flag & DXD_INLINE) {
  457. if (min_size <= sizeof (ji->i_inline_ea)) {
  458. ea_buf->flag = EA_INLINE;
  459. ea_buf->max_size = sizeof (ji->i_inline_ea);
  460. ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
  461. goto size_check;
  462. }
  463. current_blocks = 0;
  464. } else {
  465. if (!(ji->ea.flag & DXD_EXTENT)) {
  466. jfs_error(sb, "ea_get: invalid ea.flag)");
  467. return -EIO;
  468. }
  469. current_blocks = (ea_size + sb->s_blocksize - 1) >>
  470. sb->s_blocksize_bits;
  471. }
  472. size = max(min_size, ea_size);
  473. if (size > PSIZE) {
  474. /*
  475. * To keep the rest of the code simple. Allocate a
  476. * contiguous buffer to work with
  477. */
  478. ea_buf->xattr = kmalloc(size, GFP_KERNEL);
  479. if (ea_buf->xattr == NULL)
  480. return -ENOMEM;
  481. ea_buf->flag = EA_MALLOC;
  482. ea_buf->max_size = (size + sb->s_blocksize - 1) &
  483. ~(sb->s_blocksize - 1);
  484. if (ea_size == 0)
  485. return 0;
  486. if ((rc = ea_read(inode, ea_buf->xattr))) {
  487. kfree(ea_buf->xattr);
  488. ea_buf->xattr = NULL;
  489. return rc;
  490. }
  491. goto size_check;
  492. }
  493. blocks_needed = (min_size + sb->s_blocksize - 1) >>
  494. sb->s_blocksize_bits;
  495. if (blocks_needed > current_blocks) {
  496. /* Allocate new blocks to quota. */
  497. if (DQUOT_ALLOC_BLOCK(inode, blocks_needed))
  498. return -EDQUOT;
  499. quota_allocation = blocks_needed;
  500. rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
  501. &blkno);
  502. if (rc)
  503. goto clean_up;
  504. DXDlength(&ea_buf->new_ea, blocks_needed);
  505. DXDaddress(&ea_buf->new_ea, blkno);
  506. ea_buf->new_ea.flag = DXD_EXTENT;
  507. DXDsize(&ea_buf->new_ea, min_size);
  508. ea_buf->flag = EA_EXTENT | EA_NEW;
  509. ea_buf->mp = get_metapage(inode, blkno,
  510. blocks_needed << sb->s_blocksize_bits,
  511. 1);
  512. if (ea_buf->mp == NULL) {
  513. dbFree(inode, blkno, (s64) blocks_needed);
  514. rc = -EIO;
  515. goto clean_up;
  516. }
  517. ea_buf->xattr = ea_buf->mp->data;
  518. ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
  519. ~(sb->s_blocksize - 1);
  520. if (ea_size == 0)
  521. return 0;
  522. if ((rc = ea_read(inode, ea_buf->xattr))) {
  523. discard_metapage(ea_buf->mp);
  524. dbFree(inode, blkno, (s64) blocks_needed);
  525. goto clean_up;
  526. }
  527. goto size_check;
  528. }
  529. ea_buf->flag = EA_EXTENT;
  530. ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
  531. lengthDXD(&ji->ea) << sb->s_blocksize_bits,
  532. 1);
  533. if (ea_buf->mp == NULL) {
  534. rc = -EIO;
  535. goto clean_up;
  536. }
  537. ea_buf->xattr = ea_buf->mp->data;
  538. ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
  539. ~(sb->s_blocksize - 1);
  540. size_check:
  541. if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
  542. printk(KERN_ERR "ea_get: invalid extended attribute\n");
  543. dump_mem("xattr", ea_buf->xattr, ea_size);
  544. ea_release(inode, ea_buf);
  545. rc = -EIO;
  546. goto clean_up;
  547. }
  548. return ea_size;
  549. clean_up:
  550. /* Rollback quota allocation */
  551. if (quota_allocation)
  552. DQUOT_FREE_BLOCK(inode, quota_allocation);
  553. return (rc);
  554. }
  555. static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
  556. {
  557. if (ea_buf->flag & EA_MALLOC)
  558. kfree(ea_buf->xattr);
  559. else if (ea_buf->flag & EA_EXTENT) {
  560. assert(ea_buf->mp);
  561. release_metapage(ea_buf->mp);
  562. if (ea_buf->flag & EA_NEW)
  563. dbFree(inode, addressDXD(&ea_buf->new_ea),
  564. lengthDXD(&ea_buf->new_ea));
  565. }
  566. }
  567. static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
  568. int new_size)
  569. {
  570. struct jfs_inode_info *ji = JFS_IP(inode);
  571. unsigned long old_blocks, new_blocks;
  572. int rc = 0;
  573. if (new_size == 0) {
  574. ea_release(inode, ea_buf);
  575. ea_buf = NULL;
  576. } else if (ea_buf->flag & EA_INLINE) {
  577. assert(new_size <= sizeof (ji->i_inline_ea));
  578. ji->mode2 &= ~INLINEEA;
  579. ea_buf->new_ea.flag = DXD_INLINE;
  580. DXDsize(&ea_buf->new_ea, new_size);
  581. DXDaddress(&ea_buf->new_ea, 0);
  582. DXDlength(&ea_buf->new_ea, 0);
  583. } else if (ea_buf->flag & EA_MALLOC) {
  584. rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
  585. kfree(ea_buf->xattr);
  586. } else if (ea_buf->flag & EA_NEW) {
  587. /* We have already allocated a new dxd */
  588. flush_metapage(ea_buf->mp);
  589. } else {
  590. /* ->xattr must point to original ea's metapage */
  591. rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
  592. discard_metapage(ea_buf->mp);
  593. }
  594. if (rc)
  595. return rc;
  596. old_blocks = new_blocks = 0;
  597. if (ji->ea.flag & DXD_EXTENT) {
  598. invalidate_dxd_metapages(inode, ji->ea);
  599. old_blocks = lengthDXD(&ji->ea);
  600. }
  601. if (ea_buf) {
  602. txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
  603. if (ea_buf->new_ea.flag & DXD_EXTENT) {
  604. new_blocks = lengthDXD(&ea_buf->new_ea);
  605. if (ji->ea.flag & DXD_INLINE)
  606. ji->mode2 |= INLINEEA;
  607. }
  608. ji->ea = ea_buf->new_ea;
  609. } else {
  610. txEA(tid, inode, &ji->ea, NULL);
  611. if (ji->ea.flag & DXD_INLINE)
  612. ji->mode2 |= INLINEEA;
  613. ji->ea.flag = 0;
  614. ji->ea.size = 0;
  615. }
  616. /* If old blocks exist, they must be removed from quota allocation. */
  617. if (old_blocks)
  618. DQUOT_FREE_BLOCK(inode, old_blocks);
  619. inode->i_ctime = CURRENT_TIME;
  620. return 0;
  621. }
  622. /*
  623. * can_set_system_xattr
  624. *
  625. * This code is specific to the system.* namespace. It contains policy
  626. * which doesn't belong in the main xattr codepath.
  627. */
  628. static int can_set_system_xattr(struct inode *inode, const char *name,
  629. const void *value, size_t value_len)
  630. {
  631. #ifdef CONFIG_JFS_POSIX_ACL
  632. struct posix_acl *acl;
  633. int rc;
  634. if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
  635. return -EPERM;
  636. /*
  637. * POSIX_ACL_XATTR_ACCESS is tied to i_mode
  638. */
  639. if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0) {
  640. acl = posix_acl_from_xattr(value, value_len);
  641. if (IS_ERR(acl)) {
  642. rc = PTR_ERR(acl);
  643. printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
  644. rc);
  645. return rc;
  646. }
  647. if (acl) {
  648. mode_t mode = inode->i_mode;
  649. rc = posix_acl_equiv_mode(acl, &mode);
  650. posix_acl_release(acl);
  651. if (rc < 0) {
  652. printk(KERN_ERR
  653. "posix_acl_equiv_mode returned %d\n",
  654. rc);
  655. return rc;
  656. }
  657. inode->i_mode = mode;
  658. mark_inode_dirty(inode);
  659. }
  660. /*
  661. * We're changing the ACL. Get rid of the cached one
  662. */
  663. acl =JFS_IP(inode)->i_acl;
  664. if (acl != JFS_ACL_NOT_CACHED)
  665. posix_acl_release(acl);
  666. JFS_IP(inode)->i_acl = JFS_ACL_NOT_CACHED;
  667. return 0;
  668. } else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0) {
  669. acl = posix_acl_from_xattr(value, value_len);
  670. if (IS_ERR(acl)) {
  671. rc = PTR_ERR(acl);
  672. printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
  673. rc);
  674. return rc;
  675. }
  676. posix_acl_release(acl);
  677. /*
  678. * We're changing the default ACL. Get rid of the cached one
  679. */
  680. acl =JFS_IP(inode)->i_default_acl;
  681. if (acl && (acl != JFS_ACL_NOT_CACHED))
  682. posix_acl_release(acl);
  683. JFS_IP(inode)->i_default_acl = JFS_ACL_NOT_CACHED;
  684. return 0;
  685. }
  686. #endif /* CONFIG_JFS_POSIX_ACL */
  687. return -EOPNOTSUPP;
  688. }
  689. static int can_set_xattr(struct inode *inode, const char *name,
  690. const void *value, size_t value_len)
  691. {
  692. if (IS_RDONLY(inode))
  693. return -EROFS;
  694. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  695. return -EPERM;
  696. if(strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) == 0)
  697. /*
  698. * "system.*"
  699. */
  700. return can_set_system_xattr(inode, name, value, value_len);
  701. if(strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0)
  702. return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
  703. #ifdef CONFIG_JFS_SECURITY
  704. if (strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)
  705. == 0)
  706. return 0; /* Leave it to the security module */
  707. #endif
  708. if((strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) != 0) &&
  709. (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) != 0))
  710. return -EOPNOTSUPP;
  711. if (!S_ISREG(inode->i_mode) &&
  712. (!S_ISDIR(inode->i_mode) || inode->i_mode &S_ISVTX))
  713. return -EPERM;
  714. return permission(inode, MAY_WRITE, NULL);
  715. }
  716. int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
  717. const void *value, size_t value_len, int flags)
  718. {
  719. struct jfs_ea_list *ealist;
  720. struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
  721. struct ea_buffer ea_buf;
  722. int old_ea_size = 0;
  723. int xattr_size;
  724. int new_size;
  725. int namelen = strlen(name);
  726. char *os2name = NULL;
  727. int found = 0;
  728. int rc;
  729. int length;
  730. if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
  731. os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
  732. GFP_KERNEL);
  733. if (!os2name)
  734. return -ENOMEM;
  735. strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
  736. name = os2name;
  737. namelen -= XATTR_OS2_PREFIX_LEN;
  738. }
  739. down_write(&JFS_IP(inode)->xattr_sem);
  740. xattr_size = ea_get(inode, &ea_buf, 0);
  741. if (xattr_size < 0) {
  742. rc = xattr_size;
  743. goto out;
  744. }
  745. again:
  746. ealist = (struct jfs_ea_list *) ea_buf.xattr;
  747. new_size = sizeof (struct jfs_ea_list);
  748. if (xattr_size) {
  749. for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
  750. ea = NEXT_EA(ea)) {
  751. if ((namelen == ea->namelen) &&
  752. (memcmp(name, ea->name, namelen) == 0)) {
  753. found = 1;
  754. if (flags & XATTR_CREATE) {
  755. rc = -EEXIST;
  756. goto release;
  757. }
  758. old_ea = ea;
  759. old_ea_size = EA_SIZE(ea);
  760. next_ea = NEXT_EA(ea);
  761. } else
  762. new_size += EA_SIZE(ea);
  763. }
  764. }
  765. if (!found) {
  766. if (flags & XATTR_REPLACE) {
  767. rc = -ENODATA;
  768. goto release;
  769. }
  770. if (value == NULL) {
  771. rc = 0;
  772. goto release;
  773. }
  774. }
  775. if (value)
  776. new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
  777. if (new_size > ea_buf.max_size) {
  778. /*
  779. * We need to allocate more space for merged ea list.
  780. * We should only have loop to again: once.
  781. */
  782. ea_release(inode, &ea_buf);
  783. xattr_size = ea_get(inode, &ea_buf, new_size);
  784. if (xattr_size < 0) {
  785. rc = xattr_size;
  786. goto out;
  787. }
  788. goto again;
  789. }
  790. /* Remove old ea of the same name */
  791. if (found) {
  792. /* number of bytes following target EA */
  793. length = (char *) END_EALIST(ealist) - (char *) next_ea;
  794. if (length > 0)
  795. memmove(old_ea, next_ea, length);
  796. xattr_size -= old_ea_size;
  797. }
  798. /* Add new entry to the end */
  799. if (value) {
  800. if (xattr_size == 0)
  801. /* Completely new ea list */
  802. xattr_size = sizeof (struct jfs_ea_list);
  803. ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
  804. ea->flag = 0;
  805. ea->namelen = namelen;
  806. ea->valuelen = (cpu_to_le16(value_len));
  807. memcpy(ea->name, name, namelen);
  808. ea->name[namelen] = 0;
  809. if (value_len)
  810. memcpy(&ea->name[namelen + 1], value, value_len);
  811. xattr_size += EA_SIZE(ea);
  812. }
  813. /* DEBUG - If we did this right, these number match */
  814. if (xattr_size != new_size) {
  815. printk(KERN_ERR
  816. "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
  817. xattr_size, new_size);
  818. rc = -EINVAL;
  819. goto release;
  820. }
  821. /*
  822. * If we're left with an empty list, there's no ea
  823. */
  824. if (new_size == sizeof (struct jfs_ea_list))
  825. new_size = 0;
  826. ealist->size = cpu_to_le32(new_size);
  827. rc = ea_put(tid, inode, &ea_buf, new_size);
  828. goto out;
  829. release:
  830. ea_release(inode, &ea_buf);
  831. out:
  832. up_write(&JFS_IP(inode)->xattr_sem);
  833. kfree(os2name);
  834. return rc;
  835. }
  836. int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  837. size_t value_len, int flags)
  838. {
  839. struct inode *inode = dentry->d_inode;
  840. struct jfs_inode_info *ji = JFS_IP(inode);
  841. int rc;
  842. tid_t tid;
  843. if ((rc = can_set_xattr(inode, name, value, value_len)))
  844. return rc;
  845. if (value == NULL) { /* empty EA, do not remove */
  846. value = "";
  847. value_len = 0;
  848. }
  849. tid = txBegin(inode->i_sb, 0);
  850. down(&ji->commit_sem);
  851. rc = __jfs_setxattr(tid, dentry->d_inode, name, value, value_len,
  852. flags);
  853. if (!rc)
  854. rc = txCommit(tid, 1, &inode, 0);
  855. txEnd(tid);
  856. up(&ji->commit_sem);
  857. return rc;
  858. }
  859. static int can_get_xattr(struct inode *inode, const char *name)
  860. {
  861. #ifdef CONFIG_JFS_SECURITY
  862. if(strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) == 0)
  863. return 0;
  864. #endif
  865. if(strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0)
  866. return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
  867. if(strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) == 0)
  868. return 0;
  869. return permission(inode, MAY_READ, NULL);
  870. }
  871. ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
  872. size_t buf_size)
  873. {
  874. struct jfs_ea_list *ealist;
  875. struct jfs_ea *ea;
  876. struct ea_buffer ea_buf;
  877. int xattr_size;
  878. ssize_t size;
  879. int namelen = strlen(name);
  880. char *os2name = NULL;
  881. int rc;
  882. char *value;
  883. if ((rc = can_get_xattr(inode, name)))
  884. return rc;
  885. if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
  886. os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
  887. GFP_KERNEL);
  888. if (!os2name)
  889. return -ENOMEM;
  890. strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
  891. name = os2name;
  892. namelen -= XATTR_OS2_PREFIX_LEN;
  893. }
  894. down_read(&JFS_IP(inode)->xattr_sem);
  895. xattr_size = ea_get(inode, &ea_buf, 0);
  896. if (xattr_size < 0) {
  897. size = xattr_size;
  898. goto out;
  899. }
  900. if (xattr_size == 0)
  901. goto not_found;
  902. ealist = (struct jfs_ea_list *) ea_buf.xattr;
  903. /* Find the named attribute */
  904. for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
  905. if ((namelen == ea->namelen) &&
  906. memcmp(name, ea->name, namelen) == 0) {
  907. /* Found it */
  908. size = le16_to_cpu(ea->valuelen);
  909. if (!data)
  910. goto release;
  911. else if (size > buf_size) {
  912. size = -ERANGE;
  913. goto release;
  914. }
  915. value = ((char *) &ea->name) + ea->namelen + 1;
  916. memcpy(data, value, size);
  917. goto release;
  918. }
  919. not_found:
  920. size = -ENODATA;
  921. release:
  922. ea_release(inode, &ea_buf);
  923. out:
  924. up_read(&JFS_IP(inode)->xattr_sem);
  925. kfree(os2name);
  926. return size;
  927. }
  928. ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
  929. size_t buf_size)
  930. {
  931. int err;
  932. err = __jfs_getxattr(dentry->d_inode, name, data, buf_size);
  933. return err;
  934. }
  935. /*
  936. * No special permissions are needed to list attributes except for trusted.*
  937. */
  938. static inline int can_list(struct jfs_ea *ea)
  939. {
  940. return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
  941. XATTR_TRUSTED_PREFIX_LEN) ||
  942. capable(CAP_SYS_ADMIN));
  943. }
  944. ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
  945. {
  946. struct inode *inode = dentry->d_inode;
  947. char *buffer;
  948. ssize_t size = 0;
  949. int xattr_size;
  950. struct jfs_ea_list *ealist;
  951. struct jfs_ea *ea;
  952. struct ea_buffer ea_buf;
  953. down_read(&JFS_IP(inode)->xattr_sem);
  954. xattr_size = ea_get(inode, &ea_buf, 0);
  955. if (xattr_size < 0) {
  956. size = xattr_size;
  957. goto out;
  958. }
  959. if (xattr_size == 0)
  960. goto release;
  961. ealist = (struct jfs_ea_list *) ea_buf.xattr;
  962. /* compute required size of list */
  963. for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
  964. if (can_list(ea))
  965. size += name_size(ea) + 1;
  966. }
  967. if (!data)
  968. goto release;
  969. if (size > buf_size) {
  970. size = -ERANGE;
  971. goto release;
  972. }
  973. /* Copy attribute names to buffer */
  974. buffer = data;
  975. for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
  976. if (can_list(ea)) {
  977. int namelen = copy_name(buffer, ea);
  978. buffer += namelen + 1;
  979. }
  980. }
  981. release:
  982. ea_release(inode, &ea_buf);
  983. out:
  984. up_read(&JFS_IP(inode)->xattr_sem);
  985. return size;
  986. }
  987. int jfs_removexattr(struct dentry *dentry, const char *name)
  988. {
  989. struct inode *inode = dentry->d_inode;
  990. struct jfs_inode_info *ji = JFS_IP(inode);
  991. int rc;
  992. tid_t tid;
  993. if ((rc = can_set_xattr(inode, name, NULL, 0)))
  994. return rc;
  995. tid = txBegin(inode->i_sb, 0);
  996. down(&ji->commit_sem);
  997. rc = __jfs_setxattr(tid, dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
  998. if (!rc)
  999. rc = txCommit(tid, 1, &inode, 0);
  1000. txEnd(tid);
  1001. up(&ji->commit_sem);
  1002. return rc;
  1003. }
  1004. #ifdef CONFIG_JFS_SECURITY
  1005. int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir)
  1006. {
  1007. int rc;
  1008. size_t len;
  1009. void *value;
  1010. char *suffix;
  1011. char *name;
  1012. rc = security_inode_init_security(inode, dir, &suffix, &value, &len);
  1013. if (rc) {
  1014. if (rc == -EOPNOTSUPP)
  1015. return 0;
  1016. return rc;
  1017. }
  1018. name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 1 + strlen(suffix),
  1019. GFP_NOFS);
  1020. if (!name) {
  1021. rc = -ENOMEM;
  1022. goto kmalloc_failed;
  1023. }
  1024. strcpy(name, XATTR_SECURITY_PREFIX);
  1025. strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
  1026. rc = __jfs_setxattr(tid, inode, name, value, len, 0);
  1027. kfree(name);
  1028. kmalloc_failed:
  1029. kfree(suffix);
  1030. kfree(value);
  1031. return rc;
  1032. }
  1033. #endif