xattr.c 28 KB

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