xattr.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. * linux/fs/ext2/xattr.c
  3. *
  4. * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
  5. *
  6. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  7. * Extended attributes for symlinks and special files added per
  8. * suggestion of Luka Renko <luka.renko@hermes.si>.
  9. * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
  10. * Red Hat Inc.
  11. *
  12. */
  13. /*
  14. * Extended attributes are stored on disk blocks allocated outside of
  15. * any inode. The i_file_acl field is then made to point to this allocated
  16. * block. If all extended attributes of an inode are identical, these
  17. * inodes may share the same extended attribute block. Such situations
  18. * are automatically detected by keeping a cache of recent attribute block
  19. * numbers and hashes over the block's contents in memory.
  20. *
  21. *
  22. * Extended attribute block layout:
  23. *
  24. * +------------------+
  25. * | header |
  26. * | entry 1 | |
  27. * | entry 2 | | growing downwards
  28. * | entry 3 | v
  29. * | four null bytes |
  30. * | . . . |
  31. * | value 1 | ^
  32. * | value 3 | | growing upwards
  33. * | value 2 | |
  34. * +------------------+
  35. *
  36. * The block header is followed by multiple entry descriptors. These entry
  37. * descriptors are variable in size, and alligned to EXT2_XATTR_PAD
  38. * byte boundaries. The entry descriptors are sorted by attribute name,
  39. * so that two extended attribute blocks can be compared efficiently.
  40. *
  41. * Attribute values are aligned to the end of the block, stored in
  42. * no specific order. They are also padded to EXT2_XATTR_PAD byte
  43. * boundaries. No additional gaps are left between them.
  44. *
  45. * Locking strategy
  46. * ----------------
  47. * EXT2_I(inode)->i_file_acl is protected by EXT2_I(inode)->xattr_sem.
  48. * EA blocks are only changed if they are exclusive to an inode, so
  49. * holding xattr_sem also means that nothing but the EA block's reference
  50. * count will change. Multiple writers to an EA block are synchronized
  51. * by the bh lock. No more than a single bh lock is held at any time
  52. * to avoid deadlocks.
  53. */
  54. #include <linux/buffer_head.h>
  55. #include <linux/module.h>
  56. #include <linux/init.h>
  57. #include <linux/slab.h>
  58. #include <linux/mbcache.h>
  59. #include <linux/quotaops.h>
  60. #include <linux/rwsem.h>
  61. #include "ext2.h"
  62. #include "xattr.h"
  63. #include "acl.h"
  64. #define HDR(bh) ((struct ext2_xattr_header *)((bh)->b_data))
  65. #define ENTRY(ptr) ((struct ext2_xattr_entry *)(ptr))
  66. #define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
  67. #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
  68. #ifdef EXT2_XATTR_DEBUG
  69. # define ea_idebug(inode, f...) do { \
  70. printk(KERN_DEBUG "inode %s:%ld: ", \
  71. inode->i_sb->s_id, inode->i_ino); \
  72. printk(f); \
  73. printk("\n"); \
  74. } while (0)
  75. # define ea_bdebug(bh, f...) do { \
  76. char b[BDEVNAME_SIZE]; \
  77. printk(KERN_DEBUG "block %s:%lu: ", \
  78. bdevname(bh->b_bdev, b), \
  79. (unsigned long) bh->b_blocknr); \
  80. printk(f); \
  81. printk("\n"); \
  82. } while (0)
  83. #else
  84. # define ea_idebug(f...)
  85. # define ea_bdebug(f...)
  86. #endif
  87. static int ext2_xattr_set2(struct inode *, struct buffer_head *,
  88. struct ext2_xattr_header *);
  89. static int ext2_xattr_cache_insert(struct buffer_head *);
  90. static struct buffer_head *ext2_xattr_cache_find(struct inode *,
  91. struct ext2_xattr_header *);
  92. static void ext2_xattr_rehash(struct ext2_xattr_header *,
  93. struct ext2_xattr_entry *);
  94. static struct mb_cache *ext2_xattr_cache;
  95. static struct xattr_handler *ext2_xattr_handler_map[] = {
  96. [EXT2_XATTR_INDEX_USER] = &ext2_xattr_user_handler,
  97. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  98. [EXT2_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext2_xattr_acl_access_handler,
  99. [EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext2_xattr_acl_default_handler,
  100. #endif
  101. [EXT2_XATTR_INDEX_TRUSTED] = &ext2_xattr_trusted_handler,
  102. #ifdef CONFIG_EXT2_FS_SECURITY
  103. [EXT2_XATTR_INDEX_SECURITY] = &ext2_xattr_security_handler,
  104. #endif
  105. };
  106. struct xattr_handler *ext2_xattr_handlers[] = {
  107. &ext2_xattr_user_handler,
  108. &ext2_xattr_trusted_handler,
  109. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  110. &ext2_xattr_acl_access_handler,
  111. &ext2_xattr_acl_default_handler,
  112. #endif
  113. #ifdef CONFIG_EXT2_FS_SECURITY
  114. &ext2_xattr_security_handler,
  115. #endif
  116. NULL
  117. };
  118. static inline struct xattr_handler *
  119. ext2_xattr_handler(int name_index)
  120. {
  121. struct xattr_handler *handler = NULL;
  122. if (name_index > 0 && name_index < ARRAY_SIZE(ext2_xattr_handler_map))
  123. handler = ext2_xattr_handler_map[name_index];
  124. return handler;
  125. }
  126. /*
  127. * ext2_xattr_get()
  128. *
  129. * Copy an extended attribute into the buffer
  130. * provided, or compute the buffer size required.
  131. * Buffer is NULL to compute the size of the buffer required.
  132. *
  133. * Returns a negative error number on failure, or the number of bytes
  134. * used / required on success.
  135. */
  136. int
  137. ext2_xattr_get(struct inode *inode, int name_index, const char *name,
  138. void *buffer, size_t buffer_size)
  139. {
  140. struct buffer_head *bh = NULL;
  141. struct ext2_xattr_entry *entry;
  142. size_t name_len, size;
  143. char *end;
  144. int error;
  145. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  146. name_index, name, buffer, (long)buffer_size);
  147. if (name == NULL)
  148. return -EINVAL;
  149. down_read(&EXT2_I(inode)->xattr_sem);
  150. error = -ENODATA;
  151. if (!EXT2_I(inode)->i_file_acl)
  152. goto cleanup;
  153. ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
  154. bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
  155. error = -EIO;
  156. if (!bh)
  157. goto cleanup;
  158. ea_bdebug(bh, "b_count=%d, refcount=%d",
  159. atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
  160. end = bh->b_data + bh->b_size;
  161. if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
  162. HDR(bh)->h_blocks != cpu_to_le32(1)) {
  163. bad_block: ext2_error(inode->i_sb, "ext2_xattr_get",
  164. "inode %ld: bad block %d", inode->i_ino,
  165. EXT2_I(inode)->i_file_acl);
  166. error = -EIO;
  167. goto cleanup;
  168. }
  169. /* find named attribute */
  170. name_len = strlen(name);
  171. error = -ERANGE;
  172. if (name_len > 255)
  173. goto cleanup;
  174. entry = FIRST_ENTRY(bh);
  175. while (!IS_LAST_ENTRY(entry)) {
  176. struct ext2_xattr_entry *next =
  177. EXT2_XATTR_NEXT(entry);
  178. if ((char *)next >= end)
  179. goto bad_block;
  180. if (name_index == entry->e_name_index &&
  181. name_len == entry->e_name_len &&
  182. memcmp(name, entry->e_name, name_len) == 0)
  183. goto found;
  184. entry = next;
  185. }
  186. /* Check the remaining name entries */
  187. while (!IS_LAST_ENTRY(entry)) {
  188. struct ext2_xattr_entry *next =
  189. EXT2_XATTR_NEXT(entry);
  190. if ((char *)next >= end)
  191. goto bad_block;
  192. entry = next;
  193. }
  194. if (ext2_xattr_cache_insert(bh))
  195. ea_idebug(inode, "cache insert failed");
  196. error = -ENODATA;
  197. goto cleanup;
  198. found:
  199. /* check the buffer size */
  200. if (entry->e_value_block != 0)
  201. goto bad_block;
  202. size = le32_to_cpu(entry->e_value_size);
  203. if (size > inode->i_sb->s_blocksize ||
  204. le16_to_cpu(entry->e_value_offs) + size > inode->i_sb->s_blocksize)
  205. goto bad_block;
  206. if (ext2_xattr_cache_insert(bh))
  207. ea_idebug(inode, "cache insert failed");
  208. if (buffer) {
  209. error = -ERANGE;
  210. if (size > buffer_size)
  211. goto cleanup;
  212. /* return value of attribute */
  213. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  214. size);
  215. }
  216. error = size;
  217. cleanup:
  218. brelse(bh);
  219. up_read(&EXT2_I(inode)->xattr_sem);
  220. return error;
  221. }
  222. /*
  223. * ext2_xattr_list()
  224. *
  225. * Copy a list of attribute names into the buffer
  226. * provided, or compute the buffer size required.
  227. * Buffer is NULL to compute the size of the buffer required.
  228. *
  229. * Returns a negative error number on failure, or the number of bytes
  230. * used / required on success.
  231. */
  232. static int
  233. ext2_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
  234. {
  235. struct buffer_head *bh = NULL;
  236. struct ext2_xattr_entry *entry;
  237. char *end;
  238. size_t rest = buffer_size;
  239. int error;
  240. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  241. buffer, (long)buffer_size);
  242. down_read(&EXT2_I(inode)->xattr_sem);
  243. error = 0;
  244. if (!EXT2_I(inode)->i_file_acl)
  245. goto cleanup;
  246. ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
  247. bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
  248. error = -EIO;
  249. if (!bh)
  250. goto cleanup;
  251. ea_bdebug(bh, "b_count=%d, refcount=%d",
  252. atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
  253. end = bh->b_data + bh->b_size;
  254. if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
  255. HDR(bh)->h_blocks != cpu_to_le32(1)) {
  256. bad_block: ext2_error(inode->i_sb, "ext2_xattr_list",
  257. "inode %ld: bad block %d", inode->i_ino,
  258. EXT2_I(inode)->i_file_acl);
  259. error = -EIO;
  260. goto cleanup;
  261. }
  262. /* check the on-disk data structure */
  263. entry = FIRST_ENTRY(bh);
  264. while (!IS_LAST_ENTRY(entry)) {
  265. struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(entry);
  266. if ((char *)next >= end)
  267. goto bad_block;
  268. entry = next;
  269. }
  270. if (ext2_xattr_cache_insert(bh))
  271. ea_idebug(inode, "cache insert failed");
  272. /* list the attribute names */
  273. for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
  274. entry = EXT2_XATTR_NEXT(entry)) {
  275. struct xattr_handler *handler =
  276. ext2_xattr_handler(entry->e_name_index);
  277. if (handler) {
  278. size_t size = handler->list(inode, buffer, rest,
  279. entry->e_name,
  280. entry->e_name_len);
  281. if (buffer) {
  282. if (size > rest) {
  283. error = -ERANGE;
  284. goto cleanup;
  285. }
  286. buffer += size;
  287. }
  288. rest -= size;
  289. }
  290. }
  291. error = buffer_size - rest; /* total size */
  292. cleanup:
  293. brelse(bh);
  294. up_read(&EXT2_I(inode)->xattr_sem);
  295. return error;
  296. }
  297. /*
  298. * Inode operation listxattr()
  299. *
  300. * dentry->d_inode->i_mutex: don't care
  301. */
  302. ssize_t
  303. ext2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  304. {
  305. return ext2_xattr_list(dentry->d_inode, buffer, size);
  306. }
  307. /*
  308. * If the EXT2_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  309. * not set, set it.
  310. */
  311. static void ext2_xattr_update_super_block(struct super_block *sb)
  312. {
  313. if (EXT2_HAS_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR))
  314. return;
  315. EXT2_SET_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR);
  316. sb->s_dirt = 1;
  317. mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
  318. }
  319. /*
  320. * ext2_xattr_set()
  321. *
  322. * Create, replace or remove an extended attribute for this inode. Buffer
  323. * is NULL to remove an existing extended attribute, and non-NULL to
  324. * either replace an existing extended attribute, or create a new extended
  325. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  326. * specify that an extended attribute must exist and must not exist
  327. * previous to the call, respectively.
  328. *
  329. * Returns 0, or a negative error number on failure.
  330. */
  331. int
  332. ext2_xattr_set(struct inode *inode, int name_index, const char *name,
  333. const void *value, size_t value_len, int flags)
  334. {
  335. struct super_block *sb = inode->i_sb;
  336. struct buffer_head *bh = NULL;
  337. struct ext2_xattr_header *header = NULL;
  338. struct ext2_xattr_entry *here, *last;
  339. size_t name_len, free, min_offs = sb->s_blocksize;
  340. int not_found = 1, error;
  341. char *end;
  342. /*
  343. * header -- Points either into bh, or to a temporarily
  344. * allocated buffer.
  345. * here -- The named entry found, or the place for inserting, within
  346. * the block pointed to by header.
  347. * last -- Points right after the last named entry within the block
  348. * pointed to by header.
  349. * min_offs -- The offset of the first value (values are aligned
  350. * towards the end of the block).
  351. * end -- Points right after the block pointed to by header.
  352. */
  353. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  354. name_index, name, value, (long)value_len);
  355. if (value == NULL)
  356. value_len = 0;
  357. if (name == NULL)
  358. return -EINVAL;
  359. name_len = strlen(name);
  360. if (name_len > 255 || value_len > sb->s_blocksize)
  361. return -ERANGE;
  362. down_write(&EXT2_I(inode)->xattr_sem);
  363. if (EXT2_I(inode)->i_file_acl) {
  364. /* The inode already has an extended attribute block. */
  365. bh = sb_bread(sb, EXT2_I(inode)->i_file_acl);
  366. error = -EIO;
  367. if (!bh)
  368. goto cleanup;
  369. ea_bdebug(bh, "b_count=%d, refcount=%d",
  370. atomic_read(&(bh->b_count)),
  371. le32_to_cpu(HDR(bh)->h_refcount));
  372. header = HDR(bh);
  373. end = bh->b_data + bh->b_size;
  374. if (header->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
  375. header->h_blocks != cpu_to_le32(1)) {
  376. bad_block: ext2_error(sb, "ext2_xattr_set",
  377. "inode %ld: bad block %d", inode->i_ino,
  378. EXT2_I(inode)->i_file_acl);
  379. error = -EIO;
  380. goto cleanup;
  381. }
  382. /* Find the named attribute. */
  383. here = FIRST_ENTRY(bh);
  384. while (!IS_LAST_ENTRY(here)) {
  385. struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(here);
  386. if ((char *)next >= end)
  387. goto bad_block;
  388. if (!here->e_value_block && here->e_value_size) {
  389. size_t offs = le16_to_cpu(here->e_value_offs);
  390. if (offs < min_offs)
  391. min_offs = offs;
  392. }
  393. not_found = name_index - here->e_name_index;
  394. if (!not_found)
  395. not_found = name_len - here->e_name_len;
  396. if (!not_found)
  397. not_found = memcmp(name, here->e_name,name_len);
  398. if (not_found <= 0)
  399. break;
  400. here = next;
  401. }
  402. last = here;
  403. /* We still need to compute min_offs and last. */
  404. while (!IS_LAST_ENTRY(last)) {
  405. struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(last);
  406. if ((char *)next >= end)
  407. goto bad_block;
  408. if (!last->e_value_block && last->e_value_size) {
  409. size_t offs = le16_to_cpu(last->e_value_offs);
  410. if (offs < min_offs)
  411. min_offs = offs;
  412. }
  413. last = next;
  414. }
  415. /* Check whether we have enough space left. */
  416. free = min_offs - ((char*)last - (char*)header) - sizeof(__u32);
  417. } else {
  418. /* We will use a new extended attribute block. */
  419. free = sb->s_blocksize -
  420. sizeof(struct ext2_xattr_header) - sizeof(__u32);
  421. here = last = NULL; /* avoid gcc uninitialized warning. */
  422. }
  423. if (not_found) {
  424. /* Request to remove a nonexistent attribute? */
  425. error = -ENODATA;
  426. if (flags & XATTR_REPLACE)
  427. goto cleanup;
  428. error = 0;
  429. if (value == NULL)
  430. goto cleanup;
  431. } else {
  432. /* Request to create an existing attribute? */
  433. error = -EEXIST;
  434. if (flags & XATTR_CREATE)
  435. goto cleanup;
  436. if (!here->e_value_block && here->e_value_size) {
  437. size_t size = le32_to_cpu(here->e_value_size);
  438. if (le16_to_cpu(here->e_value_offs) + size >
  439. sb->s_blocksize || size > sb->s_blocksize)
  440. goto bad_block;
  441. free += EXT2_XATTR_SIZE(size);
  442. }
  443. free += EXT2_XATTR_LEN(name_len);
  444. }
  445. error = -ENOSPC;
  446. if (free < EXT2_XATTR_LEN(name_len) + EXT2_XATTR_SIZE(value_len))
  447. goto cleanup;
  448. /* Here we know that we can set the new attribute. */
  449. if (header) {
  450. struct mb_cache_entry *ce;
  451. /* assert(header == HDR(bh)); */
  452. ce = mb_cache_entry_get(ext2_xattr_cache, bh->b_bdev,
  453. bh->b_blocknr);
  454. lock_buffer(bh);
  455. if (header->h_refcount == cpu_to_le32(1)) {
  456. ea_bdebug(bh, "modifying in-place");
  457. if (ce)
  458. mb_cache_entry_free(ce);
  459. /* keep the buffer locked while modifying it. */
  460. } else {
  461. int offset;
  462. if (ce)
  463. mb_cache_entry_release(ce);
  464. unlock_buffer(bh);
  465. ea_bdebug(bh, "cloning");
  466. header = kmalloc(bh->b_size, GFP_KERNEL);
  467. error = -ENOMEM;
  468. if (header == NULL)
  469. goto cleanup;
  470. memcpy(header, HDR(bh), bh->b_size);
  471. header->h_refcount = cpu_to_le32(1);
  472. offset = (char *)here - bh->b_data;
  473. here = ENTRY((char *)header + offset);
  474. offset = (char *)last - bh->b_data;
  475. last = ENTRY((char *)header + offset);
  476. }
  477. } else {
  478. /* Allocate a buffer where we construct the new block. */
  479. header = kzalloc(sb->s_blocksize, GFP_KERNEL);
  480. error = -ENOMEM;
  481. if (header == NULL)
  482. goto cleanup;
  483. end = (char *)header + sb->s_blocksize;
  484. header->h_magic = cpu_to_le32(EXT2_XATTR_MAGIC);
  485. header->h_blocks = header->h_refcount = cpu_to_le32(1);
  486. last = here = ENTRY(header+1);
  487. }
  488. /* Iff we are modifying the block in-place, bh is locked here. */
  489. if (not_found) {
  490. /* Insert the new name. */
  491. size_t size = EXT2_XATTR_LEN(name_len);
  492. size_t rest = (char *)last - (char *)here;
  493. memmove((char *)here + size, here, rest);
  494. memset(here, 0, size);
  495. here->e_name_index = name_index;
  496. here->e_name_len = name_len;
  497. memcpy(here->e_name, name, name_len);
  498. } else {
  499. if (!here->e_value_block && here->e_value_size) {
  500. char *first_val = (char *)header + min_offs;
  501. size_t offs = le16_to_cpu(here->e_value_offs);
  502. char *val = (char *)header + offs;
  503. size_t size = EXT2_XATTR_SIZE(
  504. le32_to_cpu(here->e_value_size));
  505. if (size == EXT2_XATTR_SIZE(value_len)) {
  506. /* The old and the new value have the same
  507. size. Just replace. */
  508. here->e_value_size = cpu_to_le32(value_len);
  509. memset(val + size - EXT2_XATTR_PAD, 0,
  510. EXT2_XATTR_PAD); /* Clear pad bytes. */
  511. memcpy(val, value, value_len);
  512. goto skip_replace;
  513. }
  514. /* Remove the old value. */
  515. memmove(first_val + size, first_val, val - first_val);
  516. memset(first_val, 0, size);
  517. here->e_value_offs = 0;
  518. min_offs += size;
  519. /* Adjust all value offsets. */
  520. last = ENTRY(header+1);
  521. while (!IS_LAST_ENTRY(last)) {
  522. size_t o = le16_to_cpu(last->e_value_offs);
  523. if (!last->e_value_block && o < offs)
  524. last->e_value_offs =
  525. cpu_to_le16(o + size);
  526. last = EXT2_XATTR_NEXT(last);
  527. }
  528. }
  529. if (value == NULL) {
  530. /* Remove the old name. */
  531. size_t size = EXT2_XATTR_LEN(name_len);
  532. last = ENTRY((char *)last - size);
  533. memmove(here, (char*)here + size,
  534. (char*)last - (char*)here);
  535. memset(last, 0, size);
  536. }
  537. }
  538. if (value != NULL) {
  539. /* Insert the new value. */
  540. here->e_value_size = cpu_to_le32(value_len);
  541. if (value_len) {
  542. size_t size = EXT2_XATTR_SIZE(value_len);
  543. char *val = (char *)header + min_offs - size;
  544. here->e_value_offs =
  545. cpu_to_le16((char *)val - (char *)header);
  546. memset(val + size - EXT2_XATTR_PAD, 0,
  547. EXT2_XATTR_PAD); /* Clear the pad bytes. */
  548. memcpy(val, value, value_len);
  549. }
  550. }
  551. skip_replace:
  552. if (IS_LAST_ENTRY(ENTRY(header+1))) {
  553. /* This block is now empty. */
  554. if (bh && header == HDR(bh))
  555. unlock_buffer(bh); /* we were modifying in-place. */
  556. error = ext2_xattr_set2(inode, bh, NULL);
  557. } else {
  558. ext2_xattr_rehash(header, here);
  559. if (bh && header == HDR(bh))
  560. unlock_buffer(bh); /* we were modifying in-place. */
  561. error = ext2_xattr_set2(inode, bh, header);
  562. }
  563. cleanup:
  564. brelse(bh);
  565. if (!(bh && header == HDR(bh)))
  566. kfree(header);
  567. up_write(&EXT2_I(inode)->xattr_sem);
  568. return error;
  569. }
  570. /*
  571. * Second half of ext2_xattr_set(): Update the file system.
  572. */
  573. static int
  574. ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
  575. struct ext2_xattr_header *header)
  576. {
  577. struct super_block *sb = inode->i_sb;
  578. struct buffer_head *new_bh = NULL;
  579. int error;
  580. if (header) {
  581. new_bh = ext2_xattr_cache_find(inode, header);
  582. if (new_bh) {
  583. /* We found an identical block in the cache. */
  584. if (new_bh == old_bh) {
  585. ea_bdebug(new_bh, "keeping this block");
  586. } else {
  587. /* The old block is released after updating
  588. the inode. */
  589. ea_bdebug(new_bh, "reusing block");
  590. error = -EDQUOT;
  591. if (DQUOT_ALLOC_BLOCK(inode, 1)) {
  592. unlock_buffer(new_bh);
  593. goto cleanup;
  594. }
  595. HDR(new_bh)->h_refcount = cpu_to_le32(1 +
  596. le32_to_cpu(HDR(new_bh)->h_refcount));
  597. ea_bdebug(new_bh, "refcount now=%d",
  598. le32_to_cpu(HDR(new_bh)->h_refcount));
  599. }
  600. unlock_buffer(new_bh);
  601. } else if (old_bh && header == HDR(old_bh)) {
  602. /* Keep this block. No need to lock the block as we
  603. don't need to change the reference count. */
  604. new_bh = old_bh;
  605. get_bh(new_bh);
  606. ext2_xattr_cache_insert(new_bh);
  607. } else {
  608. /* We need to allocate a new block */
  609. int goal = le32_to_cpu(EXT2_SB(sb)->s_es->
  610. s_first_data_block) +
  611. EXT2_I(inode)->i_block_group *
  612. EXT2_BLOCKS_PER_GROUP(sb);
  613. int block = ext2_new_block(inode, goal, &error);
  614. if (error)
  615. goto cleanup;
  616. ea_idebug(inode, "creating block %d", block);
  617. new_bh = sb_getblk(sb, block);
  618. if (!new_bh) {
  619. ext2_free_blocks(inode, block, 1);
  620. error = -EIO;
  621. goto cleanup;
  622. }
  623. lock_buffer(new_bh);
  624. memcpy(new_bh->b_data, header, new_bh->b_size);
  625. set_buffer_uptodate(new_bh);
  626. unlock_buffer(new_bh);
  627. ext2_xattr_cache_insert(new_bh);
  628. ext2_xattr_update_super_block(sb);
  629. }
  630. mark_buffer_dirty(new_bh);
  631. if (IS_SYNC(inode)) {
  632. sync_dirty_buffer(new_bh);
  633. error = -EIO;
  634. if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
  635. goto cleanup;
  636. }
  637. }
  638. /* Update the inode. */
  639. EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  640. inode->i_ctime = CURRENT_TIME_SEC;
  641. if (IS_SYNC(inode)) {
  642. error = ext2_sync_inode (inode);
  643. /* In case sync failed due to ENOSPC the inode was actually
  644. * written (only some dirty data were not) so we just proceed
  645. * as if nothing happened and cleanup the unused block */
  646. if (error && error != -ENOSPC) {
  647. if (new_bh && new_bh != old_bh)
  648. DQUOT_FREE_BLOCK(inode, 1);
  649. goto cleanup;
  650. }
  651. } else
  652. mark_inode_dirty(inode);
  653. error = 0;
  654. if (old_bh && old_bh != new_bh) {
  655. struct mb_cache_entry *ce;
  656. /*
  657. * If there was an old block and we are no longer using it,
  658. * release the old block.
  659. */
  660. ce = mb_cache_entry_get(ext2_xattr_cache, old_bh->b_bdev,
  661. old_bh->b_blocknr);
  662. lock_buffer(old_bh);
  663. if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
  664. /* Free the old block. */
  665. if (ce)
  666. mb_cache_entry_free(ce);
  667. ea_bdebug(old_bh, "freeing");
  668. ext2_free_blocks(inode, old_bh->b_blocknr, 1);
  669. /* We let our caller release old_bh, so we
  670. * need to duplicate the buffer before. */
  671. get_bh(old_bh);
  672. bforget(old_bh);
  673. } else {
  674. /* Decrement the refcount only. */
  675. HDR(old_bh)->h_refcount = cpu_to_le32(
  676. le32_to_cpu(HDR(old_bh)->h_refcount) - 1);
  677. if (ce)
  678. mb_cache_entry_release(ce);
  679. DQUOT_FREE_BLOCK(inode, 1);
  680. mark_buffer_dirty(old_bh);
  681. ea_bdebug(old_bh, "refcount now=%d",
  682. le32_to_cpu(HDR(old_bh)->h_refcount));
  683. }
  684. unlock_buffer(old_bh);
  685. }
  686. cleanup:
  687. brelse(new_bh);
  688. return error;
  689. }
  690. /*
  691. * ext2_xattr_delete_inode()
  692. *
  693. * Free extended attribute resources associated with this inode. This
  694. * is called immediately before an inode is freed.
  695. */
  696. void
  697. ext2_xattr_delete_inode(struct inode *inode)
  698. {
  699. struct buffer_head *bh = NULL;
  700. struct mb_cache_entry *ce;
  701. down_write(&EXT2_I(inode)->xattr_sem);
  702. if (!EXT2_I(inode)->i_file_acl)
  703. goto cleanup;
  704. bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
  705. if (!bh) {
  706. ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
  707. "inode %ld: block %d read error", inode->i_ino,
  708. EXT2_I(inode)->i_file_acl);
  709. goto cleanup;
  710. }
  711. ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
  712. if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
  713. HDR(bh)->h_blocks != cpu_to_le32(1)) {
  714. ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
  715. "inode %ld: bad block %d", inode->i_ino,
  716. EXT2_I(inode)->i_file_acl);
  717. goto cleanup;
  718. }
  719. ce = mb_cache_entry_get(ext2_xattr_cache, bh->b_bdev, bh->b_blocknr);
  720. lock_buffer(bh);
  721. if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
  722. if (ce)
  723. mb_cache_entry_free(ce);
  724. ext2_free_blocks(inode, EXT2_I(inode)->i_file_acl, 1);
  725. get_bh(bh);
  726. bforget(bh);
  727. unlock_buffer(bh);
  728. } else {
  729. HDR(bh)->h_refcount = cpu_to_le32(
  730. le32_to_cpu(HDR(bh)->h_refcount) - 1);
  731. if (ce)
  732. mb_cache_entry_release(ce);
  733. ea_bdebug(bh, "refcount now=%d",
  734. le32_to_cpu(HDR(bh)->h_refcount));
  735. unlock_buffer(bh);
  736. mark_buffer_dirty(bh);
  737. if (IS_SYNC(inode))
  738. sync_dirty_buffer(bh);
  739. DQUOT_FREE_BLOCK(inode, 1);
  740. }
  741. EXT2_I(inode)->i_file_acl = 0;
  742. cleanup:
  743. brelse(bh);
  744. up_write(&EXT2_I(inode)->xattr_sem);
  745. }
  746. /*
  747. * ext2_xattr_put_super()
  748. *
  749. * This is called when a file system is unmounted.
  750. */
  751. void
  752. ext2_xattr_put_super(struct super_block *sb)
  753. {
  754. mb_cache_shrink(sb->s_bdev);
  755. }
  756. /*
  757. * ext2_xattr_cache_insert()
  758. *
  759. * Create a new entry in the extended attribute cache, and insert
  760. * it unless such an entry is already in the cache.
  761. *
  762. * Returns 0, or a negative error number on failure.
  763. */
  764. static int
  765. ext2_xattr_cache_insert(struct buffer_head *bh)
  766. {
  767. __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
  768. struct mb_cache_entry *ce;
  769. int error;
  770. ce = mb_cache_entry_alloc(ext2_xattr_cache);
  771. if (!ce)
  772. return -ENOMEM;
  773. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
  774. if (error) {
  775. mb_cache_entry_free(ce);
  776. if (error == -EBUSY) {
  777. ea_bdebug(bh, "already in cache (%d cache entries)",
  778. atomic_read(&ext2_xattr_cache->c_entry_count));
  779. error = 0;
  780. }
  781. } else {
  782. ea_bdebug(bh, "inserting [%x] (%d cache entries)", (int)hash,
  783. atomic_read(&ext2_xattr_cache->c_entry_count));
  784. mb_cache_entry_release(ce);
  785. }
  786. return error;
  787. }
  788. /*
  789. * ext2_xattr_cmp()
  790. *
  791. * Compare two extended attribute blocks for equality.
  792. *
  793. * Returns 0 if the blocks are equal, 1 if they differ, and
  794. * a negative error number on errors.
  795. */
  796. static int
  797. ext2_xattr_cmp(struct ext2_xattr_header *header1,
  798. struct ext2_xattr_header *header2)
  799. {
  800. struct ext2_xattr_entry *entry1, *entry2;
  801. entry1 = ENTRY(header1+1);
  802. entry2 = ENTRY(header2+1);
  803. while (!IS_LAST_ENTRY(entry1)) {
  804. if (IS_LAST_ENTRY(entry2))
  805. return 1;
  806. if (entry1->e_hash != entry2->e_hash ||
  807. entry1->e_name_index != entry2->e_name_index ||
  808. entry1->e_name_len != entry2->e_name_len ||
  809. entry1->e_value_size != entry2->e_value_size ||
  810. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  811. return 1;
  812. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  813. return -EIO;
  814. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  815. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  816. le32_to_cpu(entry1->e_value_size)))
  817. return 1;
  818. entry1 = EXT2_XATTR_NEXT(entry1);
  819. entry2 = EXT2_XATTR_NEXT(entry2);
  820. }
  821. if (!IS_LAST_ENTRY(entry2))
  822. return 1;
  823. return 0;
  824. }
  825. /*
  826. * ext2_xattr_cache_find()
  827. *
  828. * Find an identical extended attribute block.
  829. *
  830. * Returns a locked buffer head to the block found, or NULL if such
  831. * a block was not found or an error occurred.
  832. */
  833. static struct buffer_head *
  834. ext2_xattr_cache_find(struct inode *inode, struct ext2_xattr_header *header)
  835. {
  836. __u32 hash = le32_to_cpu(header->h_hash);
  837. struct mb_cache_entry *ce;
  838. if (!header->h_hash)
  839. return NULL; /* never share */
  840. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  841. again:
  842. ce = mb_cache_entry_find_first(ext2_xattr_cache, 0,
  843. inode->i_sb->s_bdev, hash);
  844. while (ce) {
  845. struct buffer_head *bh;
  846. if (IS_ERR(ce)) {
  847. if (PTR_ERR(ce) == -EAGAIN)
  848. goto again;
  849. break;
  850. }
  851. bh = sb_bread(inode->i_sb, ce->e_block);
  852. if (!bh) {
  853. ext2_error(inode->i_sb, "ext2_xattr_cache_find",
  854. "inode %ld: block %ld read error",
  855. inode->i_ino, (unsigned long) ce->e_block);
  856. } else {
  857. lock_buffer(bh);
  858. if (le32_to_cpu(HDR(bh)->h_refcount) >
  859. EXT2_XATTR_REFCOUNT_MAX) {
  860. ea_idebug(inode, "block %ld refcount %d>%d",
  861. (unsigned long) ce->e_block,
  862. le32_to_cpu(HDR(bh)->h_refcount),
  863. EXT2_XATTR_REFCOUNT_MAX);
  864. } else if (!ext2_xattr_cmp(header, HDR(bh))) {
  865. ea_bdebug(bh, "b_count=%d",
  866. atomic_read(&(bh->b_count)));
  867. mb_cache_entry_release(ce);
  868. return bh;
  869. }
  870. unlock_buffer(bh);
  871. brelse(bh);
  872. }
  873. ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
  874. }
  875. return NULL;
  876. }
  877. #define NAME_HASH_SHIFT 5
  878. #define VALUE_HASH_SHIFT 16
  879. /*
  880. * ext2_xattr_hash_entry()
  881. *
  882. * Compute the hash of an extended attribute.
  883. */
  884. static inline void ext2_xattr_hash_entry(struct ext2_xattr_header *header,
  885. struct ext2_xattr_entry *entry)
  886. {
  887. __u32 hash = 0;
  888. char *name = entry->e_name;
  889. int n;
  890. for (n=0; n < entry->e_name_len; n++) {
  891. hash = (hash << NAME_HASH_SHIFT) ^
  892. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  893. *name++;
  894. }
  895. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  896. __le32 *value = (__le32 *)((char *)header +
  897. le16_to_cpu(entry->e_value_offs));
  898. for (n = (le32_to_cpu(entry->e_value_size) +
  899. EXT2_XATTR_ROUND) >> EXT2_XATTR_PAD_BITS; n; n--) {
  900. hash = (hash << VALUE_HASH_SHIFT) ^
  901. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  902. le32_to_cpu(*value++);
  903. }
  904. }
  905. entry->e_hash = cpu_to_le32(hash);
  906. }
  907. #undef NAME_HASH_SHIFT
  908. #undef VALUE_HASH_SHIFT
  909. #define BLOCK_HASH_SHIFT 16
  910. /*
  911. * ext2_xattr_rehash()
  912. *
  913. * Re-compute the extended attribute hash value after an entry has changed.
  914. */
  915. static void ext2_xattr_rehash(struct ext2_xattr_header *header,
  916. struct ext2_xattr_entry *entry)
  917. {
  918. struct ext2_xattr_entry *here;
  919. __u32 hash = 0;
  920. ext2_xattr_hash_entry(header, entry);
  921. here = ENTRY(header+1);
  922. while (!IS_LAST_ENTRY(here)) {
  923. if (!here->e_hash) {
  924. /* Block is not shared if an entry's hash value == 0 */
  925. hash = 0;
  926. break;
  927. }
  928. hash = (hash << BLOCK_HASH_SHIFT) ^
  929. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  930. le32_to_cpu(here->e_hash);
  931. here = EXT2_XATTR_NEXT(here);
  932. }
  933. header->h_hash = cpu_to_le32(hash);
  934. }
  935. #undef BLOCK_HASH_SHIFT
  936. int __init
  937. init_ext2_xattr(void)
  938. {
  939. ext2_xattr_cache = mb_cache_create("ext2_xattr", NULL,
  940. sizeof(struct mb_cache_entry) +
  941. sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
  942. if (!ext2_xattr_cache)
  943. return -ENOMEM;
  944. return 0;
  945. }
  946. void
  947. exit_ext2_xattr(void)
  948. {
  949. mb_cache_destroy(ext2_xattr_cache);
  950. }