xattr.c 28 KB

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