original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1125 lines
27 KiB

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