xattr.c 29 KB

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