xattr.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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 (vfs_dq_alloc_block(ip, nblocks)) {
  242. return -EDQUOT;
  243. }
  244. rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
  245. if (rc) {
  246. /*Rollback quota allocation. */
  247. vfs_dq_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. vfs_dq_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 (vfs_dq_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. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1,
  534. ea_buf->xattr, ea_size, 1);
  535. ea_release(inode, ea_buf);
  536. rc = -EIO;
  537. goto clean_up;
  538. }
  539. return ea_size;
  540. clean_up:
  541. /* Rollback quota allocation */
  542. if (quota_allocation)
  543. vfs_dq_free_block(inode, quota_allocation);
  544. return (rc);
  545. }
  546. static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
  547. {
  548. if (ea_buf->flag & EA_MALLOC)
  549. kfree(ea_buf->xattr);
  550. else if (ea_buf->flag & EA_EXTENT) {
  551. assert(ea_buf->mp);
  552. release_metapage(ea_buf->mp);
  553. if (ea_buf->flag & EA_NEW)
  554. dbFree(inode, addressDXD(&ea_buf->new_ea),
  555. lengthDXD(&ea_buf->new_ea));
  556. }
  557. }
  558. static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
  559. int new_size)
  560. {
  561. struct jfs_inode_info *ji = JFS_IP(inode);
  562. unsigned long old_blocks, new_blocks;
  563. int rc = 0;
  564. if (new_size == 0) {
  565. ea_release(inode, ea_buf);
  566. ea_buf = NULL;
  567. } else if (ea_buf->flag & EA_INLINE) {
  568. assert(new_size <= sizeof (ji->i_inline_ea));
  569. ji->mode2 &= ~INLINEEA;
  570. ea_buf->new_ea.flag = DXD_INLINE;
  571. DXDsize(&ea_buf->new_ea, new_size);
  572. DXDaddress(&ea_buf->new_ea, 0);
  573. DXDlength(&ea_buf->new_ea, 0);
  574. } else if (ea_buf->flag & EA_MALLOC) {
  575. rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
  576. kfree(ea_buf->xattr);
  577. } else if (ea_buf->flag & EA_NEW) {
  578. /* We have already allocated a new dxd */
  579. flush_metapage(ea_buf->mp);
  580. } else {
  581. /* ->xattr must point to original ea's metapage */
  582. rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
  583. discard_metapage(ea_buf->mp);
  584. }
  585. if (rc)
  586. return rc;
  587. old_blocks = new_blocks = 0;
  588. if (ji->ea.flag & DXD_EXTENT) {
  589. invalidate_dxd_metapages(inode, ji->ea);
  590. old_blocks = lengthDXD(&ji->ea);
  591. }
  592. if (ea_buf) {
  593. txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
  594. if (ea_buf->new_ea.flag & DXD_EXTENT) {
  595. new_blocks = lengthDXD(&ea_buf->new_ea);
  596. if (ji->ea.flag & DXD_INLINE)
  597. ji->mode2 |= INLINEEA;
  598. }
  599. ji->ea = ea_buf->new_ea;
  600. } else {
  601. txEA(tid, inode, &ji->ea, NULL);
  602. if (ji->ea.flag & DXD_INLINE)
  603. ji->mode2 |= INLINEEA;
  604. ji->ea.flag = 0;
  605. ji->ea.size = 0;
  606. }
  607. /* If old blocks exist, they must be removed from quota allocation. */
  608. if (old_blocks)
  609. vfs_dq_free_block(inode, old_blocks);
  610. inode->i_ctime = CURRENT_TIME;
  611. return 0;
  612. }
  613. /*
  614. * can_set_system_xattr
  615. *
  616. * This code is specific to the system.* namespace. It contains policy
  617. * which doesn't belong in the main xattr codepath.
  618. */
  619. static int can_set_system_xattr(struct inode *inode, const char *name,
  620. const void *value, size_t value_len)
  621. {
  622. #ifdef CONFIG_JFS_POSIX_ACL
  623. struct posix_acl *acl;
  624. int rc;
  625. if (!is_owner_or_cap(inode))
  626. return -EPERM;
  627. /*
  628. * POSIX_ACL_XATTR_ACCESS is tied to i_mode
  629. */
  630. if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0) {
  631. acl = posix_acl_from_xattr(value, value_len);
  632. if (IS_ERR(acl)) {
  633. rc = PTR_ERR(acl);
  634. printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
  635. rc);
  636. return rc;
  637. }
  638. if (acl) {
  639. mode_t mode = inode->i_mode;
  640. rc = posix_acl_equiv_mode(acl, &mode);
  641. posix_acl_release(acl);
  642. if (rc < 0) {
  643. printk(KERN_ERR
  644. "posix_acl_equiv_mode returned %d\n",
  645. rc);
  646. return rc;
  647. }
  648. inode->i_mode = mode;
  649. mark_inode_dirty(inode);
  650. }
  651. /*
  652. * We're changing the ACL. Get rid of the cached one
  653. */
  654. acl =JFS_IP(inode)->i_acl;
  655. if (acl != JFS_ACL_NOT_CACHED)
  656. posix_acl_release(acl);
  657. JFS_IP(inode)->i_acl = JFS_ACL_NOT_CACHED;
  658. return 0;
  659. } else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0) {
  660. acl = posix_acl_from_xattr(value, value_len);
  661. if (IS_ERR(acl)) {
  662. rc = PTR_ERR(acl);
  663. printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
  664. rc);
  665. return rc;
  666. }
  667. posix_acl_release(acl);
  668. /*
  669. * We're changing the default ACL. Get rid of the cached one
  670. */
  671. acl =JFS_IP(inode)->i_default_acl;
  672. if (acl && (acl != JFS_ACL_NOT_CACHED))
  673. posix_acl_release(acl);
  674. JFS_IP(inode)->i_default_acl = JFS_ACL_NOT_CACHED;
  675. return 0;
  676. }
  677. #endif /* CONFIG_JFS_POSIX_ACL */
  678. return -EOPNOTSUPP;
  679. }
  680. /*
  681. * Most of the permission checking is done by xattr_permission in the vfs.
  682. * The local file system is responsible for handling the system.* namespace.
  683. * We also need to verify that this is a namespace that we recognize.
  684. */
  685. static int can_set_xattr(struct inode *inode, const char *name,
  686. const void *value, size_t value_len)
  687. {
  688. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  689. return can_set_system_xattr(inode, name, value, value_len);
  690. /*
  691. * Don't allow setting an attribute in an unknown namespace.
  692. */
  693. if (strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) &&
  694. strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
  695. strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
  696. strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN))
  697. return -EOPNOTSUPP;
  698. return 0;
  699. }
  700. int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
  701. const void *value, size_t value_len, int flags)
  702. {
  703. struct jfs_ea_list *ealist;
  704. struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
  705. struct ea_buffer ea_buf;
  706. int old_ea_size = 0;
  707. int xattr_size;
  708. int new_size;
  709. int namelen = strlen(name);
  710. char *os2name = NULL;
  711. int found = 0;
  712. int rc;
  713. int length;
  714. if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
  715. os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
  716. GFP_KERNEL);
  717. if (!os2name)
  718. return -ENOMEM;
  719. strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
  720. name = os2name;
  721. namelen -= XATTR_OS2_PREFIX_LEN;
  722. }
  723. down_write(&JFS_IP(inode)->xattr_sem);
  724. xattr_size = ea_get(inode, &ea_buf, 0);
  725. if (xattr_size < 0) {
  726. rc = xattr_size;
  727. goto out;
  728. }
  729. again:
  730. ealist = (struct jfs_ea_list *) ea_buf.xattr;
  731. new_size = sizeof (struct jfs_ea_list);
  732. if (xattr_size) {
  733. for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
  734. ea = NEXT_EA(ea)) {
  735. if ((namelen == ea->namelen) &&
  736. (memcmp(name, ea->name, namelen) == 0)) {
  737. found = 1;
  738. if (flags & XATTR_CREATE) {
  739. rc = -EEXIST;
  740. goto release;
  741. }
  742. old_ea = ea;
  743. old_ea_size = EA_SIZE(ea);
  744. next_ea = NEXT_EA(ea);
  745. } else
  746. new_size += EA_SIZE(ea);
  747. }
  748. }
  749. if (!found) {
  750. if (flags & XATTR_REPLACE) {
  751. rc = -ENODATA;
  752. goto release;
  753. }
  754. if (value == NULL) {
  755. rc = 0;
  756. goto release;
  757. }
  758. }
  759. if (value)
  760. new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
  761. if (new_size > ea_buf.max_size) {
  762. /*
  763. * We need to allocate more space for merged ea list.
  764. * We should only have loop to again: once.
  765. */
  766. ea_release(inode, &ea_buf);
  767. xattr_size = ea_get(inode, &ea_buf, new_size);
  768. if (xattr_size < 0) {
  769. rc = xattr_size;
  770. goto out;
  771. }
  772. goto again;
  773. }
  774. /* Remove old ea of the same name */
  775. if (found) {
  776. /* number of bytes following target EA */
  777. length = (char *) END_EALIST(ealist) - (char *) next_ea;
  778. if (length > 0)
  779. memmove(old_ea, next_ea, length);
  780. xattr_size -= old_ea_size;
  781. }
  782. /* Add new entry to the end */
  783. if (value) {
  784. if (xattr_size == 0)
  785. /* Completely new ea list */
  786. xattr_size = sizeof (struct jfs_ea_list);
  787. ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
  788. ea->flag = 0;
  789. ea->namelen = namelen;
  790. ea->valuelen = (cpu_to_le16(value_len));
  791. memcpy(ea->name, name, namelen);
  792. ea->name[namelen] = 0;
  793. if (value_len)
  794. memcpy(&ea->name[namelen + 1], value, value_len);
  795. xattr_size += EA_SIZE(ea);
  796. }
  797. /* DEBUG - If we did this right, these number match */
  798. if (xattr_size != new_size) {
  799. printk(KERN_ERR
  800. "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
  801. xattr_size, new_size);
  802. rc = -EINVAL;
  803. goto release;
  804. }
  805. /*
  806. * If we're left with an empty list, there's no ea
  807. */
  808. if (new_size == sizeof (struct jfs_ea_list))
  809. new_size = 0;
  810. ealist->size = cpu_to_le32(new_size);
  811. rc = ea_put(tid, inode, &ea_buf, new_size);
  812. goto out;
  813. release:
  814. ea_release(inode, &ea_buf);
  815. out:
  816. up_write(&JFS_IP(inode)->xattr_sem);
  817. kfree(os2name);
  818. return rc;
  819. }
  820. int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  821. size_t value_len, int flags)
  822. {
  823. struct inode *inode = dentry->d_inode;
  824. struct jfs_inode_info *ji = JFS_IP(inode);
  825. int rc;
  826. tid_t tid;
  827. if ((rc = can_set_xattr(inode, name, value, value_len)))
  828. return rc;
  829. if (value == NULL) { /* empty EA, do not remove */
  830. value = "";
  831. value_len = 0;
  832. }
  833. tid = txBegin(inode->i_sb, 0);
  834. mutex_lock(&ji->commit_mutex);
  835. rc = __jfs_setxattr(tid, dentry->d_inode, name, value, value_len,
  836. flags);
  837. if (!rc)
  838. rc = txCommit(tid, 1, &inode, 0);
  839. txEnd(tid);
  840. mutex_unlock(&ji->commit_mutex);
  841. return rc;
  842. }
  843. ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
  844. size_t buf_size)
  845. {
  846. struct jfs_ea_list *ealist;
  847. struct jfs_ea *ea;
  848. struct ea_buffer ea_buf;
  849. int xattr_size;
  850. ssize_t size;
  851. int namelen = strlen(name);
  852. char *os2name = NULL;
  853. char *value;
  854. if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
  855. os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
  856. GFP_KERNEL);
  857. if (!os2name)
  858. return -ENOMEM;
  859. strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
  860. name = os2name;
  861. namelen -= XATTR_OS2_PREFIX_LEN;
  862. }
  863. down_read(&JFS_IP(inode)->xattr_sem);
  864. xattr_size = ea_get(inode, &ea_buf, 0);
  865. if (xattr_size < 0) {
  866. size = xattr_size;
  867. goto out;
  868. }
  869. if (xattr_size == 0)
  870. goto not_found;
  871. ealist = (struct jfs_ea_list *) ea_buf.xattr;
  872. /* Find the named attribute */
  873. for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
  874. if ((namelen == ea->namelen) &&
  875. memcmp(name, ea->name, namelen) == 0) {
  876. /* Found it */
  877. size = le16_to_cpu(ea->valuelen);
  878. if (!data)
  879. goto release;
  880. else if (size > buf_size) {
  881. size = -ERANGE;
  882. goto release;
  883. }
  884. value = ((char *) &ea->name) + ea->namelen + 1;
  885. memcpy(data, value, size);
  886. goto release;
  887. }
  888. not_found:
  889. size = -ENODATA;
  890. release:
  891. ea_release(inode, &ea_buf);
  892. out:
  893. up_read(&JFS_IP(inode)->xattr_sem);
  894. kfree(os2name);
  895. return size;
  896. }
  897. ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
  898. size_t buf_size)
  899. {
  900. int err;
  901. err = __jfs_getxattr(dentry->d_inode, name, data, buf_size);
  902. return err;
  903. }
  904. /*
  905. * No special permissions are needed to list attributes except for trusted.*
  906. */
  907. static inline int can_list(struct jfs_ea *ea)
  908. {
  909. return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
  910. XATTR_TRUSTED_PREFIX_LEN) ||
  911. capable(CAP_SYS_ADMIN));
  912. }
  913. ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
  914. {
  915. struct inode *inode = dentry->d_inode;
  916. char *buffer;
  917. ssize_t size = 0;
  918. int xattr_size;
  919. struct jfs_ea_list *ealist;
  920. struct jfs_ea *ea;
  921. struct ea_buffer ea_buf;
  922. down_read(&JFS_IP(inode)->xattr_sem);
  923. xattr_size = ea_get(inode, &ea_buf, 0);
  924. if (xattr_size < 0) {
  925. size = xattr_size;
  926. goto out;
  927. }
  928. if (xattr_size == 0)
  929. goto release;
  930. ealist = (struct jfs_ea_list *) ea_buf.xattr;
  931. /* compute required size of list */
  932. for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
  933. if (can_list(ea))
  934. size += name_size(ea) + 1;
  935. }
  936. if (!data)
  937. goto release;
  938. if (size > buf_size) {
  939. size = -ERANGE;
  940. goto release;
  941. }
  942. /* Copy attribute names to buffer */
  943. buffer = data;
  944. for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
  945. if (can_list(ea)) {
  946. int namelen = copy_name(buffer, ea);
  947. buffer += namelen + 1;
  948. }
  949. }
  950. release:
  951. ea_release(inode, &ea_buf);
  952. out:
  953. up_read(&JFS_IP(inode)->xattr_sem);
  954. return size;
  955. }
  956. int jfs_removexattr(struct dentry *dentry, const char *name)
  957. {
  958. struct inode *inode = dentry->d_inode;
  959. struct jfs_inode_info *ji = JFS_IP(inode);
  960. int rc;
  961. tid_t tid;
  962. if ((rc = can_set_xattr(inode, name, NULL, 0)))
  963. return rc;
  964. tid = txBegin(inode->i_sb, 0);
  965. mutex_lock(&ji->commit_mutex);
  966. rc = __jfs_setxattr(tid, dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
  967. if (!rc)
  968. rc = txCommit(tid, 1, &inode, 0);
  969. txEnd(tid);
  970. mutex_unlock(&ji->commit_mutex);
  971. return rc;
  972. }
  973. #ifdef CONFIG_JFS_SECURITY
  974. int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir)
  975. {
  976. int rc;
  977. size_t len;
  978. void *value;
  979. char *suffix;
  980. char *name;
  981. rc = security_inode_init_security(inode, dir, &suffix, &value, &len);
  982. if (rc) {
  983. if (rc == -EOPNOTSUPP)
  984. return 0;
  985. return rc;
  986. }
  987. name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 1 + strlen(suffix),
  988. GFP_NOFS);
  989. if (!name) {
  990. rc = -ENOMEM;
  991. goto kmalloc_failed;
  992. }
  993. strcpy(name, XATTR_SECURITY_PREFIX);
  994. strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
  995. rc = __jfs_setxattr(tid, inode, name, value, len, 0);
  996. kfree(name);
  997. kmalloc_failed:
  998. kfree(suffix);
  999. kfree(value);
  1000. return rc;
  1001. }
  1002. #endif