xattr.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. /*
  84. * These three routines are used to recognize on-disk extended attributes
  85. * that are in a recognized namespace. If the attribute is not recognized,
  86. * "os2." is prepended to the name
  87. */
  88. static inline int is_os2_xattr(struct jfs_ea *ea)
  89. {
  90. /*
  91. * Check for "system."
  92. */
  93. if ((ea->namelen >= XATTR_SYSTEM_PREFIX_LEN) &&
  94. !strncmp(ea->name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  95. return false;
  96. /*
  97. * Check for "user."
  98. */
  99. if ((ea->namelen >= XATTR_USER_PREFIX_LEN) &&
  100. !strncmp(ea->name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
  101. return false;
  102. /*
  103. * Check for "security."
  104. */
  105. if ((ea->namelen >= XATTR_SECURITY_PREFIX_LEN) &&
  106. !strncmp(ea->name, XATTR_SECURITY_PREFIX,
  107. XATTR_SECURITY_PREFIX_LEN))
  108. return false;
  109. /*
  110. * Check for "trusted."
  111. */
  112. if ((ea->namelen >= XATTR_TRUSTED_PREFIX_LEN) &&
  113. !strncmp(ea->name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
  114. return false;
  115. /*
  116. * Add any other valid namespace prefixes here
  117. */
  118. /*
  119. * We assume it's OS/2's flat namespace
  120. */
  121. return true;
  122. }
  123. static inline int name_size(struct jfs_ea *ea)
  124. {
  125. if (is_os2_xattr(ea))
  126. return ea->namelen + XATTR_OS2_PREFIX_LEN;
  127. else
  128. return ea->namelen;
  129. }
  130. static inline int copy_name(char *buffer, struct jfs_ea *ea)
  131. {
  132. int len = ea->namelen;
  133. if (is_os2_xattr(ea)) {
  134. memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
  135. buffer += XATTR_OS2_PREFIX_LEN;
  136. len += XATTR_OS2_PREFIX_LEN;
  137. }
  138. memcpy(buffer, ea->name, ea->namelen);
  139. buffer[ea->namelen] = 0;
  140. return len;
  141. }
  142. /* Forward references */
  143. static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
  144. /*
  145. * NAME: ea_write_inline
  146. *
  147. * FUNCTION: Attempt to write an EA inline if area is available
  148. *
  149. * PRE CONDITIONS:
  150. * Already verified that the specified EA is small enough to fit inline
  151. *
  152. * PARAMETERS:
  153. * ip - Inode pointer
  154. * ealist - EA list pointer
  155. * size - size of ealist in bytes
  156. * ea - dxd_t structure to be filled in with necessary EA information
  157. * if we successfully copy the EA inline
  158. *
  159. * NOTES:
  160. * Checks if the inode's inline area is available. If so, copies EA inline
  161. * and sets <ea> fields appropriately. Otherwise, returns failure, EA will
  162. * have to be put into an extent.
  163. *
  164. * RETURNS: 0 for successful copy to inline area; -1 if area not available
  165. */
  166. static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
  167. int size, dxd_t * ea)
  168. {
  169. struct jfs_inode_info *ji = JFS_IP(ip);
  170. /*
  171. * Make sure we have an EA -- the NULL EA list is valid, but you
  172. * can't copy it!
  173. */
  174. if (ealist && size > sizeof (struct jfs_ea_list)) {
  175. assert(size <= sizeof (ji->i_inline_ea));
  176. /*
  177. * See if the space is available or if it is already being
  178. * used for an inline EA.
  179. */
  180. if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
  181. return -EPERM;
  182. DXDsize(ea, size);
  183. DXDlength(ea, 0);
  184. DXDaddress(ea, 0);
  185. memcpy(ji->i_inline_ea, ealist, size);
  186. ea->flag = DXD_INLINE;
  187. ji->mode2 &= ~INLINEEA;
  188. } else {
  189. ea->flag = 0;
  190. DXDsize(ea, 0);
  191. DXDlength(ea, 0);
  192. DXDaddress(ea, 0);
  193. /* Free up INLINE area */
  194. if (ji->ea.flag & DXD_INLINE)
  195. ji->mode2 |= INLINEEA;
  196. }
  197. return 0;
  198. }
  199. /*
  200. * NAME: ea_write
  201. *
  202. * FUNCTION: Write an EA for an inode
  203. *
  204. * PRE CONDITIONS: EA has been verified
  205. *
  206. * PARAMETERS:
  207. * ip - Inode pointer
  208. * ealist - EA list pointer
  209. * size - size of ealist in bytes
  210. * ea - dxd_t structure to be filled in appropriately with where the
  211. * EA was copied
  212. *
  213. * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
  214. * extent and synchronously writes it to those blocks.
  215. *
  216. * RETURNS: 0 for success; Anything else indicates failure
  217. */
  218. static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
  219. dxd_t * ea)
  220. {
  221. struct super_block *sb = ip->i_sb;
  222. struct jfs_inode_info *ji = JFS_IP(ip);
  223. struct jfs_sb_info *sbi = JFS_SBI(sb);
  224. int nblocks;
  225. s64 blkno;
  226. int rc = 0, i;
  227. char *cp;
  228. s32 nbytes, nb;
  229. s32 bytes_to_write;
  230. struct metapage *mp;
  231. /*
  232. * Quick check to see if this is an in-linable EA. Short EAs
  233. * and empty EAs are all in-linable, provided the space exists.
  234. */
  235. if (!ealist || size <= sizeof (ji->i_inline_ea)) {
  236. if (!ea_write_inline(ip, ealist, size, ea))
  237. return 0;
  238. }
  239. /* figure out how many blocks we need */
  240. nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
  241. /* Allocate new blocks to quota. */
  242. rc = dquot_alloc_block(ip, nblocks);
  243. if (rc)
  244. return rc;
  245. rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
  246. if (rc) {
  247. /*Rollback quota allocation. */
  248. dquot_free_block(ip, nblocks);
  249. return rc;
  250. }
  251. /*
  252. * Now have nblocks worth of storage to stuff into the FEALIST.
  253. * loop over the FEALIST copying data into the buffer one page at
  254. * a time.
  255. */
  256. cp = (char *) ealist;
  257. nbytes = size;
  258. for (i = 0; i < nblocks; i += sbi->nbperpage) {
  259. /*
  260. * Determine how many bytes for this request, and round up to
  261. * the nearest aggregate block size
  262. */
  263. nb = min(PSIZE, nbytes);
  264. bytes_to_write =
  265. ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
  266. << sb->s_blocksize_bits;
  267. if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
  268. rc = -EIO;
  269. goto failed;
  270. }
  271. memcpy(mp->data, cp, nb);
  272. /*
  273. * We really need a way to propagate errors for
  274. * forced writes like this one. --hch
  275. *
  276. * (__write_metapage => release_metapage => flush_metapage)
  277. */
  278. #ifdef _JFS_FIXME
  279. if ((rc = flush_metapage(mp))) {
  280. /*
  281. * the write failed -- this means that the buffer
  282. * is still assigned and the blocks are not being
  283. * used. this seems like the best error recovery
  284. * we can get ...
  285. */
  286. goto failed;
  287. }
  288. #else
  289. flush_metapage(mp);
  290. #endif
  291. cp += PSIZE;
  292. nbytes -= nb;
  293. }
  294. ea->flag = DXD_EXTENT;
  295. DXDsize(ea, le32_to_cpu(ealist->size));
  296. DXDlength(ea, nblocks);
  297. DXDaddress(ea, blkno);
  298. /* Free up INLINE area */
  299. if (ji->ea.flag & DXD_INLINE)
  300. ji->mode2 |= INLINEEA;
  301. return 0;
  302. failed:
  303. /* Rollback quota allocation. */
  304. dquot_free_block(ip, nblocks);
  305. dbFree(ip, blkno, nblocks);
  306. return rc;
  307. }
  308. /*
  309. * NAME: ea_read_inline
  310. *
  311. * FUNCTION: Read an inlined EA into user's buffer
  312. *
  313. * PARAMETERS:
  314. * ip - Inode pointer
  315. * ealist - Pointer to buffer to fill in with EA
  316. *
  317. * RETURNS: 0
  318. */
  319. static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
  320. {
  321. struct jfs_inode_info *ji = JFS_IP(ip);
  322. int ea_size = sizeDXD(&ji->ea);
  323. if (ea_size == 0) {
  324. ealist->size = 0;
  325. return 0;
  326. }
  327. /* Sanity Check */
  328. if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
  329. return -EIO;
  330. if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
  331. != ea_size)
  332. return -EIO;
  333. memcpy(ealist, ji->i_inline_ea, ea_size);
  334. return 0;
  335. }
  336. /*
  337. * NAME: ea_read
  338. *
  339. * FUNCTION: copy EA data into user's buffer
  340. *
  341. * PARAMETERS:
  342. * ip - Inode pointer
  343. * ealist - Pointer to buffer to fill in with EA
  344. *
  345. * NOTES: If EA is inline calls ea_read_inline() to copy EA.
  346. *
  347. * RETURNS: 0 for success; other indicates failure
  348. */
  349. static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
  350. {
  351. struct super_block *sb = ip->i_sb;
  352. struct jfs_inode_info *ji = JFS_IP(ip);
  353. struct jfs_sb_info *sbi = JFS_SBI(sb);
  354. int nblocks;
  355. s64 blkno;
  356. char *cp = (char *) ealist;
  357. int i;
  358. int nbytes, nb;
  359. s32 bytes_to_read;
  360. struct metapage *mp;
  361. /* quick check for in-line EA */
  362. if (ji->ea.flag & DXD_INLINE)
  363. return ea_read_inline(ip, ealist);
  364. nbytes = sizeDXD(&ji->ea);
  365. if (!nbytes) {
  366. jfs_error(sb, "ea_read: nbytes is 0");
  367. return -EIO;
  368. }
  369. /*
  370. * Figure out how many blocks were allocated when this EA list was
  371. * originally written to disk.
  372. */
  373. nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
  374. blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
  375. /*
  376. * I have found the disk blocks which were originally used to store
  377. * the FEALIST. now i loop over each contiguous block copying the
  378. * data into the buffer.
  379. */
  380. for (i = 0; i < nblocks; i += sbi->nbperpage) {
  381. /*
  382. * Determine how many bytes for this request, and round up to
  383. * the nearest aggregate block size
  384. */
  385. nb = min(PSIZE, nbytes);
  386. bytes_to_read =
  387. ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
  388. << sb->s_blocksize_bits;
  389. if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
  390. return -EIO;
  391. memcpy(cp, mp->data, nb);
  392. release_metapage(mp);
  393. cp += PSIZE;
  394. nbytes -= nb;
  395. }
  396. return 0;
  397. }
  398. /*
  399. * NAME: ea_get
  400. *
  401. * FUNCTION: Returns buffer containing existing extended attributes.
  402. * The size of the buffer will be the larger of the existing
  403. * attributes size, or min_size.
  404. *
  405. * The buffer, which may be inlined in the inode or in the
  406. * page cache must be release by calling ea_release or ea_put
  407. *
  408. * PARAMETERS:
  409. * inode - Inode pointer
  410. * ea_buf - Structure to be populated with ealist and its metadata
  411. * min_size- minimum size of buffer to be returned
  412. *
  413. * RETURNS: 0 for success; Other indicates failure
  414. */
  415. static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
  416. {
  417. struct jfs_inode_info *ji = JFS_IP(inode);
  418. struct super_block *sb = inode->i_sb;
  419. int size;
  420. int ea_size = sizeDXD(&ji->ea);
  421. int blocks_needed, current_blocks;
  422. s64 blkno;
  423. int rc;
  424. int quota_allocation = 0;
  425. /* When fsck.jfs clears a bad ea, it doesn't clear the size */
  426. if (ji->ea.flag == 0)
  427. ea_size = 0;
  428. if (ea_size == 0) {
  429. if (min_size == 0) {
  430. ea_buf->flag = 0;
  431. ea_buf->max_size = 0;
  432. ea_buf->xattr = NULL;
  433. return 0;
  434. }
  435. if ((min_size <= sizeof (ji->i_inline_ea)) &&
  436. (ji->mode2 & INLINEEA)) {
  437. ea_buf->flag = EA_INLINE | EA_NEW;
  438. ea_buf->max_size = sizeof (ji->i_inline_ea);
  439. ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
  440. DXDlength(&ea_buf->new_ea, 0);
  441. DXDaddress(&ea_buf->new_ea, 0);
  442. ea_buf->new_ea.flag = DXD_INLINE;
  443. DXDsize(&ea_buf->new_ea, min_size);
  444. return 0;
  445. }
  446. current_blocks = 0;
  447. } else if (ji->ea.flag & DXD_INLINE) {
  448. if (min_size <= sizeof (ji->i_inline_ea)) {
  449. ea_buf->flag = EA_INLINE;
  450. ea_buf->max_size = sizeof (ji->i_inline_ea);
  451. ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
  452. goto size_check;
  453. }
  454. current_blocks = 0;
  455. } else {
  456. if (!(ji->ea.flag & DXD_EXTENT)) {
  457. jfs_error(sb, "ea_get: invalid ea.flag)");
  458. return -EIO;
  459. }
  460. current_blocks = (ea_size + sb->s_blocksize - 1) >>
  461. sb->s_blocksize_bits;
  462. }
  463. size = max(min_size, ea_size);
  464. if (size > PSIZE) {
  465. /*
  466. * To keep the rest of the code simple. Allocate a
  467. * contiguous buffer to work with
  468. */
  469. ea_buf->xattr = kmalloc(size, GFP_KERNEL);
  470. if (ea_buf->xattr == NULL)
  471. return -ENOMEM;
  472. ea_buf->flag = EA_MALLOC;
  473. ea_buf->max_size = (size + sb->s_blocksize - 1) &
  474. ~(sb->s_blocksize - 1);
  475. if (ea_size == 0)
  476. return 0;
  477. if ((rc = ea_read(inode, ea_buf->xattr))) {
  478. kfree(ea_buf->xattr);
  479. ea_buf->xattr = NULL;
  480. return rc;
  481. }
  482. goto size_check;
  483. }
  484. blocks_needed = (min_size + sb->s_blocksize - 1) >>
  485. sb->s_blocksize_bits;
  486. if (blocks_needed > current_blocks) {
  487. /* Allocate new blocks to quota. */
  488. rc = dquot_alloc_block(inode, blocks_needed);
  489. if (rc)
  490. return -EDQUOT;
  491. quota_allocation = blocks_needed;
  492. rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
  493. &blkno);
  494. if (rc)
  495. goto clean_up;
  496. DXDlength(&ea_buf->new_ea, blocks_needed);
  497. DXDaddress(&ea_buf->new_ea, blkno);
  498. ea_buf->new_ea.flag = DXD_EXTENT;
  499. DXDsize(&ea_buf->new_ea, min_size);
  500. ea_buf->flag = EA_EXTENT | EA_NEW;
  501. ea_buf->mp = get_metapage(inode, blkno,
  502. blocks_needed << sb->s_blocksize_bits,
  503. 1);
  504. if (ea_buf->mp == NULL) {
  505. dbFree(inode, blkno, (s64) blocks_needed);
  506. rc = -EIO;
  507. goto clean_up;
  508. }
  509. ea_buf->xattr = ea_buf->mp->data;
  510. ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
  511. ~(sb->s_blocksize - 1);
  512. if (ea_size == 0)
  513. return 0;
  514. if ((rc = ea_read(inode, ea_buf->xattr))) {
  515. discard_metapage(ea_buf->mp);
  516. dbFree(inode, blkno, (s64) blocks_needed);
  517. goto clean_up;
  518. }
  519. goto size_check;
  520. }
  521. ea_buf->flag = EA_EXTENT;
  522. ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
  523. lengthDXD(&ji->ea) << sb->s_blocksize_bits,
  524. 1);
  525. if (ea_buf->mp == NULL) {
  526. rc = -EIO;
  527. goto clean_up;
  528. }
  529. ea_buf->xattr = ea_buf->mp->data;
  530. ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
  531. ~(sb->s_blocksize - 1);
  532. size_check:
  533. if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
  534. printk(KERN_ERR "ea_get: invalid extended attribute\n");
  535. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1,
  536. ea_buf->xattr, ea_size, 1);
  537. ea_release(inode, ea_buf);
  538. rc = -EIO;
  539. goto clean_up;
  540. }
  541. return ea_size;
  542. clean_up:
  543. /* Rollback quota allocation */
  544. if (quota_allocation)
  545. dquot_free_block(inode, quota_allocation);
  546. return (rc);
  547. }
  548. static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
  549. {
  550. if (ea_buf->flag & EA_MALLOC)
  551. kfree(ea_buf->xattr);
  552. else if (ea_buf->flag & EA_EXTENT) {
  553. assert(ea_buf->mp);
  554. release_metapage(ea_buf->mp);
  555. if (ea_buf->flag & EA_NEW)
  556. dbFree(inode, addressDXD(&ea_buf->new_ea),
  557. lengthDXD(&ea_buf->new_ea));
  558. }
  559. }
  560. static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
  561. int new_size)
  562. {
  563. struct jfs_inode_info *ji = JFS_IP(inode);
  564. unsigned long old_blocks, new_blocks;
  565. int rc = 0;
  566. if (new_size == 0) {
  567. ea_release(inode, ea_buf);
  568. ea_buf = NULL;
  569. } else if (ea_buf->flag & EA_INLINE) {
  570. assert(new_size <= sizeof (ji->i_inline_ea));
  571. ji->mode2 &= ~INLINEEA;
  572. ea_buf->new_ea.flag = DXD_INLINE;
  573. DXDsize(&ea_buf->new_ea, new_size);
  574. DXDaddress(&ea_buf->new_ea, 0);
  575. DXDlength(&ea_buf->new_ea, 0);
  576. } else if (ea_buf->flag & EA_MALLOC) {
  577. rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
  578. kfree(ea_buf->xattr);
  579. } else if (ea_buf->flag & EA_NEW) {
  580. /* We have already allocated a new dxd */
  581. flush_metapage(ea_buf->mp);
  582. } else {
  583. /* ->xattr must point to original ea's metapage */
  584. rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
  585. discard_metapage(ea_buf->mp);
  586. }
  587. if (rc)
  588. return rc;
  589. old_blocks = new_blocks = 0;
  590. if (ji->ea.flag & DXD_EXTENT) {
  591. invalidate_dxd_metapages(inode, ji->ea);
  592. old_blocks = lengthDXD(&ji->ea);
  593. }
  594. if (ea_buf) {
  595. txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
  596. if (ea_buf->new_ea.flag & DXD_EXTENT) {
  597. new_blocks = lengthDXD(&ea_buf->new_ea);
  598. if (ji->ea.flag & DXD_INLINE)
  599. ji->mode2 |= INLINEEA;
  600. }
  601. ji->ea = ea_buf->new_ea;
  602. } else {
  603. txEA(tid, inode, &ji->ea, NULL);
  604. if (ji->ea.flag & DXD_INLINE)
  605. ji->mode2 |= INLINEEA;
  606. ji->ea.flag = 0;
  607. ji->ea.size = 0;
  608. }
  609. /* If old blocks exist, they must be removed from quota allocation. */
  610. if (old_blocks)
  611. dquot_free_block(inode, old_blocks);
  612. inode->i_ctime = CURRENT_TIME;
  613. return 0;
  614. }
  615. /*
  616. * can_set_system_xattr
  617. *
  618. * This code is specific to the system.* namespace. It contains policy
  619. * which doesn't belong in the main xattr codepath.
  620. */
  621. static int can_set_system_xattr(struct inode *inode, const char *name,
  622. const void *value, size_t value_len)
  623. {
  624. #ifdef CONFIG_JFS_POSIX_ACL
  625. struct posix_acl *acl;
  626. int rc;
  627. if (!is_owner_or_cap(inode))
  628. return -EPERM;
  629. /*
  630. * POSIX_ACL_XATTR_ACCESS is tied to i_mode
  631. */
  632. if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0) {
  633. acl = posix_acl_from_xattr(value, value_len);
  634. if (IS_ERR(acl)) {
  635. rc = PTR_ERR(acl);
  636. printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
  637. rc);
  638. return rc;
  639. }
  640. if (acl) {
  641. mode_t mode = inode->i_mode;
  642. rc = posix_acl_equiv_mode(acl, &mode);
  643. posix_acl_release(acl);
  644. if (rc < 0) {
  645. printk(KERN_ERR
  646. "posix_acl_equiv_mode returned %d\n",
  647. rc);
  648. return rc;
  649. }
  650. inode->i_mode = mode;
  651. mark_inode_dirty(inode);
  652. }
  653. /*
  654. * We're changing the ACL. Get rid of the cached one
  655. */
  656. forget_cached_acl(inode, ACL_TYPE_ACCESS);
  657. return 0;
  658. } else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0) {
  659. acl = posix_acl_from_xattr(value, value_len);
  660. if (IS_ERR(acl)) {
  661. rc = PTR_ERR(acl);
  662. printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
  663. rc);
  664. return rc;
  665. }
  666. posix_acl_release(acl);
  667. /*
  668. * We're changing the default ACL. Get rid of the cached one
  669. */
  670. forget_cached_acl(inode, ACL_TYPE_DEFAULT);
  671. return 0;
  672. }
  673. #endif /* CONFIG_JFS_POSIX_ACL */
  674. return -EOPNOTSUPP;
  675. }
  676. /*
  677. * Most of the permission checking is done by xattr_permission in the vfs.
  678. * The local file system is responsible for handling the system.* namespace.
  679. * We also need to verify that this is a namespace that we recognize.
  680. */
  681. static int can_set_xattr(struct inode *inode, const char *name,
  682. const void *value, size_t value_len)
  683. {
  684. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  685. return can_set_system_xattr(inode, name, value, value_len);
  686. /*
  687. * Don't allow setting an attribute in an unknown namespace.
  688. */
  689. if (strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) &&
  690. strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
  691. strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
  692. strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN))
  693. return -EOPNOTSUPP;
  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. mutex_lock(&ji->commit_mutex);
  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. mutex_unlock(&ji->commit_mutex);
  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. mutex_lock(&ji->commit_mutex);
  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. mutex_unlock(&ji->commit_mutex);
  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