xattr.c 28 KB

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