xattr.c 28 KB

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