xattr.c 28 KB

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