xattr.c 29 KB

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