xattr.c 25 KB

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