xattr.c 28 KB

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