xattr.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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. static int can_set_xattr(struct inode *inode, const char *name,
  680. const void *value, size_t value_len)
  681. {
  682. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  683. return can_set_system_xattr(inode, name, value, value_len);
  684. /*
  685. * Don't allow setting an attribute in an unknown namespace.
  686. */
  687. if (strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) &&
  688. strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
  689. strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
  690. strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN))
  691. return -EOPNOTSUPP;
  692. if (!S_ISREG(inode->i_mode) &&
  693. (!S_ISDIR(inode->i_mode) || inode->i_mode &S_ISVTX))
  694. return -EPERM;
  695. return 0;
  696. }
  697. int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
  698. const void *value, size_t value_len, int flags)
  699. {
  700. struct jfs_ea_list *ealist;
  701. struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
  702. struct ea_buffer ea_buf;
  703. int old_ea_size = 0;
  704. int xattr_size;
  705. int new_size;
  706. int namelen = strlen(name);
  707. char *os2name = NULL;
  708. int found = 0;
  709. int rc;
  710. int length;
  711. if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
  712. os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
  713. GFP_KERNEL);
  714. if (!os2name)
  715. return -ENOMEM;
  716. strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
  717. name = os2name;
  718. namelen -= XATTR_OS2_PREFIX_LEN;
  719. }
  720. down_write(&JFS_IP(inode)->xattr_sem);
  721. xattr_size = ea_get(inode, &ea_buf, 0);
  722. if (xattr_size < 0) {
  723. rc = xattr_size;
  724. goto out;
  725. }
  726. again:
  727. ealist = (struct jfs_ea_list *) ea_buf.xattr;
  728. new_size = sizeof (struct jfs_ea_list);
  729. if (xattr_size) {
  730. for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
  731. ea = NEXT_EA(ea)) {
  732. if ((namelen == ea->namelen) &&
  733. (memcmp(name, ea->name, namelen) == 0)) {
  734. found = 1;
  735. if (flags & XATTR_CREATE) {
  736. rc = -EEXIST;
  737. goto release;
  738. }
  739. old_ea = ea;
  740. old_ea_size = EA_SIZE(ea);
  741. next_ea = NEXT_EA(ea);
  742. } else
  743. new_size += EA_SIZE(ea);
  744. }
  745. }
  746. if (!found) {
  747. if (flags & XATTR_REPLACE) {
  748. rc = -ENODATA;
  749. goto release;
  750. }
  751. if (value == NULL) {
  752. rc = 0;
  753. goto release;
  754. }
  755. }
  756. if (value)
  757. new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
  758. if (new_size > ea_buf.max_size) {
  759. /*
  760. * We need to allocate more space for merged ea list.
  761. * We should only have loop to again: once.
  762. */
  763. ea_release(inode, &ea_buf);
  764. xattr_size = ea_get(inode, &ea_buf, new_size);
  765. if (xattr_size < 0) {
  766. rc = xattr_size;
  767. goto out;
  768. }
  769. goto again;
  770. }
  771. /* Remove old ea of the same name */
  772. if (found) {
  773. /* number of bytes following target EA */
  774. length = (char *) END_EALIST(ealist) - (char *) next_ea;
  775. if (length > 0)
  776. memmove(old_ea, next_ea, length);
  777. xattr_size -= old_ea_size;
  778. }
  779. /* Add new entry to the end */
  780. if (value) {
  781. if (xattr_size == 0)
  782. /* Completely new ea list */
  783. xattr_size = sizeof (struct jfs_ea_list);
  784. ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
  785. ea->flag = 0;
  786. ea->namelen = namelen;
  787. ea->valuelen = (cpu_to_le16(value_len));
  788. memcpy(ea->name, name, namelen);
  789. ea->name[namelen] = 0;
  790. if (value_len)
  791. memcpy(&ea->name[namelen + 1], value, value_len);
  792. xattr_size += EA_SIZE(ea);
  793. }
  794. /* DEBUG - If we did this right, these number match */
  795. if (xattr_size != new_size) {
  796. printk(KERN_ERR
  797. "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
  798. xattr_size, new_size);
  799. rc = -EINVAL;
  800. goto release;
  801. }
  802. /*
  803. * If we're left with an empty list, there's no ea
  804. */
  805. if (new_size == sizeof (struct jfs_ea_list))
  806. new_size = 0;
  807. ealist->size = cpu_to_le32(new_size);
  808. rc = ea_put(tid, inode, &ea_buf, new_size);
  809. goto out;
  810. release:
  811. ea_release(inode, &ea_buf);
  812. out:
  813. up_write(&JFS_IP(inode)->xattr_sem);
  814. kfree(os2name);
  815. return rc;
  816. }
  817. int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  818. size_t value_len, int flags)
  819. {
  820. struct inode *inode = dentry->d_inode;
  821. struct jfs_inode_info *ji = JFS_IP(inode);
  822. int rc;
  823. tid_t tid;
  824. if ((rc = can_set_xattr(inode, name, value, value_len)))
  825. return rc;
  826. if (value == NULL) { /* empty EA, do not remove */
  827. value = "";
  828. value_len = 0;
  829. }
  830. tid = txBegin(inode->i_sb, 0);
  831. mutex_lock(&ji->commit_mutex);
  832. rc = __jfs_setxattr(tid, dentry->d_inode, name, value, value_len,
  833. flags);
  834. if (!rc)
  835. rc = txCommit(tid, 1, &inode, 0);
  836. txEnd(tid);
  837. mutex_unlock(&ji->commit_mutex);
  838. return rc;
  839. }
  840. ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
  841. size_t buf_size)
  842. {
  843. struct jfs_ea_list *ealist;
  844. struct jfs_ea *ea;
  845. struct ea_buffer ea_buf;
  846. int xattr_size;
  847. ssize_t size;
  848. int namelen = strlen(name);
  849. char *os2name = NULL;
  850. char *value;
  851. if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
  852. os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
  853. GFP_KERNEL);
  854. if (!os2name)
  855. return -ENOMEM;
  856. strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
  857. name = os2name;
  858. namelen -= XATTR_OS2_PREFIX_LEN;
  859. }
  860. down_read(&JFS_IP(inode)->xattr_sem);
  861. xattr_size = ea_get(inode, &ea_buf, 0);
  862. if (xattr_size < 0) {
  863. size = xattr_size;
  864. goto out;
  865. }
  866. if (xattr_size == 0)
  867. goto not_found;
  868. ealist = (struct jfs_ea_list *) ea_buf.xattr;
  869. /* Find the named attribute */
  870. for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
  871. if ((namelen == ea->namelen) &&
  872. memcmp(name, ea->name, namelen) == 0) {
  873. /* Found it */
  874. size = le16_to_cpu(ea->valuelen);
  875. if (!data)
  876. goto release;
  877. else if (size > buf_size) {
  878. size = -ERANGE;
  879. goto release;
  880. }
  881. value = ((char *) &ea->name) + ea->namelen + 1;
  882. memcpy(data, value, size);
  883. goto release;
  884. }
  885. not_found:
  886. size = -ENODATA;
  887. release:
  888. ea_release(inode, &ea_buf);
  889. out:
  890. up_read(&JFS_IP(inode)->xattr_sem);
  891. kfree(os2name);
  892. return size;
  893. }
  894. ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
  895. size_t buf_size)
  896. {
  897. int err;
  898. err = __jfs_getxattr(dentry->d_inode, name, data, buf_size);
  899. return err;
  900. }
  901. /*
  902. * No special permissions are needed to list attributes except for trusted.*
  903. */
  904. static inline int can_list(struct jfs_ea *ea)
  905. {
  906. return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
  907. XATTR_TRUSTED_PREFIX_LEN) ||
  908. capable(CAP_SYS_ADMIN));
  909. }
  910. ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
  911. {
  912. struct inode *inode = dentry->d_inode;
  913. char *buffer;
  914. ssize_t size = 0;
  915. int xattr_size;
  916. struct jfs_ea_list *ealist;
  917. struct jfs_ea *ea;
  918. struct ea_buffer ea_buf;
  919. down_read(&JFS_IP(inode)->xattr_sem);
  920. xattr_size = ea_get(inode, &ea_buf, 0);
  921. if (xattr_size < 0) {
  922. size = xattr_size;
  923. goto out;
  924. }
  925. if (xattr_size == 0)
  926. goto release;
  927. ealist = (struct jfs_ea_list *) ea_buf.xattr;
  928. /* compute required size of list */
  929. for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
  930. if (can_list(ea))
  931. size += name_size(ea) + 1;
  932. }
  933. if (!data)
  934. goto release;
  935. if (size > buf_size) {
  936. size = -ERANGE;
  937. goto release;
  938. }
  939. /* Copy attribute names to buffer */
  940. buffer = data;
  941. for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
  942. if (can_list(ea)) {
  943. int namelen = copy_name(buffer, ea);
  944. buffer += namelen + 1;
  945. }
  946. }
  947. release:
  948. ea_release(inode, &ea_buf);
  949. out:
  950. up_read(&JFS_IP(inode)->xattr_sem);
  951. return size;
  952. }
  953. int jfs_removexattr(struct dentry *dentry, const char *name)
  954. {
  955. struct inode *inode = dentry->d_inode;
  956. struct jfs_inode_info *ji = JFS_IP(inode);
  957. int rc;
  958. tid_t tid;
  959. if ((rc = can_set_xattr(inode, name, NULL, 0)))
  960. return rc;
  961. tid = txBegin(inode->i_sb, 0);
  962. mutex_lock(&ji->commit_mutex);
  963. rc = __jfs_setxattr(tid, dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
  964. if (!rc)
  965. rc = txCommit(tid, 1, &inode, 0);
  966. txEnd(tid);
  967. mutex_unlock(&ji->commit_mutex);
  968. return rc;
  969. }
  970. #ifdef CONFIG_JFS_SECURITY
  971. int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir)
  972. {
  973. int rc;
  974. size_t len;
  975. void *value;
  976. char *suffix;
  977. char *name;
  978. rc = security_inode_init_security(inode, dir, &suffix, &value, &len);
  979. if (rc) {
  980. if (rc == -EOPNOTSUPP)
  981. return 0;
  982. return rc;
  983. }
  984. name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 1 + strlen(suffix),
  985. GFP_NOFS);
  986. if (!name) {
  987. rc = -ENOMEM;
  988. goto kmalloc_failed;
  989. }
  990. strcpy(name, XATTR_SECURITY_PREFIX);
  991. strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
  992. rc = __jfs_setxattr(tid, inode, name, value, len, 0);
  993. kfree(name);
  994. kmalloc_failed:
  995. kfree(suffix);
  996. kfree(value);
  997. return rc;
  998. }
  999. #endif