xattr.c 28 KB

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