xattr.c 27 KB

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