xattr.c 29 KB

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