xattr.c 28 KB

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