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