xattr.c 28 KB

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