xattr.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  1. /*
  2. * linux/fs/ext4/xattr.c
  3. *
  4. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5. *
  6. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  7. * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
  8. * Extended attributes for symlinks and special files added per
  9. * suggestion of Luka Renko <luka.renko@hermes.si>.
  10. * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
  11. * Red Hat Inc.
  12. * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
  13. * and Andreas Gruenbacher <agruen@suse.de>.
  14. */
  15. /*
  16. * Extended attributes are stored directly in inodes (on file systems with
  17. * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
  18. * field contains the block number if an inode uses an additional block. All
  19. * attributes must fit in the inode and one additional block. Blocks that
  20. * contain the identical set of attributes may be shared among several inodes.
  21. * Identical blocks are detected by keeping a cache of blocks that have
  22. * recently been accessed.
  23. *
  24. * The attributes in inodes and on blocks have a different header; the entries
  25. * are stored in the same format:
  26. *
  27. * +------------------+
  28. * | header |
  29. * | entry 1 | |
  30. * | entry 2 | | growing downwards
  31. * | entry 3 | v
  32. * | four null bytes |
  33. * | . . . |
  34. * | value 1 | ^
  35. * | value 3 | | growing upwards
  36. * | value 2 | |
  37. * +------------------+
  38. *
  39. * The header is followed by multiple entry descriptors. In disk blocks, the
  40. * entry descriptors are kept sorted. In inodes, they are unsorted. The
  41. * attribute values are aligned to the end of the block in no specific order.
  42. *
  43. * Locking strategy
  44. * ----------------
  45. * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
  46. * EA blocks are only changed if they are exclusive to an inode, so
  47. * holding xattr_sem also means that nothing but the EA block's reference
  48. * count can change. Multiple writers to the same block are synchronized
  49. * by the buffer lock.
  50. */
  51. #include <linux/init.h>
  52. #include <linux/fs.h>
  53. #include <linux/slab.h>
  54. #include <linux/mbcache.h>
  55. #include <linux/quotaops.h>
  56. #include <linux/rwsem.h>
  57. #include "ext4_jbd2.h"
  58. #include "ext4.h"
  59. #include "xattr.h"
  60. #include "acl.h"
  61. #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
  62. #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
  63. #define BFIRST(bh) ENTRY(BHDR(bh)+1)
  64. #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
  65. #ifdef EXT4_XATTR_DEBUG
  66. # define ea_idebug(inode, f...) do { \
  67. printk(KERN_DEBUG "inode %s:%lu: ", \
  68. inode->i_sb->s_id, inode->i_ino); \
  69. printk(f); \
  70. printk("\n"); \
  71. } while (0)
  72. # define ea_bdebug(bh, f...) do { \
  73. char b[BDEVNAME_SIZE]; \
  74. printk(KERN_DEBUG "block %s:%lu: ", \
  75. bdevname(bh->b_bdev, b), \
  76. (unsigned long) bh->b_blocknr); \
  77. printk(f); \
  78. printk("\n"); \
  79. } while (0)
  80. #else
  81. # define ea_idebug(f...)
  82. # define ea_bdebug(f...)
  83. #endif
  84. static void ext4_xattr_cache_insert(struct buffer_head *);
  85. static struct buffer_head *ext4_xattr_cache_find(struct inode *,
  86. struct ext4_xattr_header *,
  87. struct mb_cache_entry **);
  88. static void ext4_xattr_rehash(struct ext4_xattr_header *,
  89. struct ext4_xattr_entry *);
  90. static int ext4_xattr_list(struct dentry *dentry, char *buffer,
  91. size_t buffer_size);
  92. static struct mb_cache *ext4_xattr_cache;
  93. static const struct xattr_handler *ext4_xattr_handler_map[] = {
  94. [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
  95. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  96. [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext4_xattr_acl_access_handler,
  97. [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler,
  98. #endif
  99. [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
  100. #ifdef CONFIG_EXT4_FS_SECURITY
  101. [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
  102. #endif
  103. };
  104. const struct xattr_handler *ext4_xattr_handlers[] = {
  105. &ext4_xattr_user_handler,
  106. &ext4_xattr_trusted_handler,
  107. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  108. &ext4_xattr_acl_access_handler,
  109. &ext4_xattr_acl_default_handler,
  110. #endif
  111. #ifdef CONFIG_EXT4_FS_SECURITY
  112. &ext4_xattr_security_handler,
  113. #endif
  114. NULL
  115. };
  116. static inline const struct xattr_handler *
  117. ext4_xattr_handler(int name_index)
  118. {
  119. const struct xattr_handler *handler = NULL;
  120. if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
  121. handler = ext4_xattr_handler_map[name_index];
  122. return handler;
  123. }
  124. /*
  125. * Inode operation listxattr()
  126. *
  127. * dentry->d_inode->i_mutex: don't care
  128. */
  129. ssize_t
  130. ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
  131. {
  132. return ext4_xattr_list(dentry, buffer, size);
  133. }
  134. static int
  135. ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
  136. {
  137. while (!IS_LAST_ENTRY(entry)) {
  138. struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
  139. if ((void *)next >= end)
  140. return -EIO;
  141. entry = next;
  142. }
  143. return 0;
  144. }
  145. static inline int
  146. ext4_xattr_check_block(struct buffer_head *bh)
  147. {
  148. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  149. BHDR(bh)->h_blocks != cpu_to_le32(1))
  150. return -EIO;
  151. return ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
  152. }
  153. static inline int
  154. ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
  155. {
  156. size_t value_size = le32_to_cpu(entry->e_value_size);
  157. if (entry->e_value_block != 0 || value_size > size ||
  158. le16_to_cpu(entry->e_value_offs) + value_size > size)
  159. return -EIO;
  160. return 0;
  161. }
  162. static int
  163. ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
  164. const char *name, size_t size, int sorted)
  165. {
  166. struct ext4_xattr_entry *entry;
  167. size_t name_len;
  168. int cmp = 1;
  169. if (name == NULL)
  170. return -EINVAL;
  171. name_len = strlen(name);
  172. entry = *pentry;
  173. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  174. cmp = name_index - entry->e_name_index;
  175. if (!cmp)
  176. cmp = name_len - entry->e_name_len;
  177. if (!cmp)
  178. cmp = memcmp(name, entry->e_name, name_len);
  179. if (cmp <= 0 && (sorted || cmp == 0))
  180. break;
  181. }
  182. *pentry = entry;
  183. if (!cmp && ext4_xattr_check_entry(entry, size))
  184. return -EIO;
  185. return cmp ? -ENODATA : 0;
  186. }
  187. static int
  188. ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
  189. void *buffer, size_t buffer_size)
  190. {
  191. struct buffer_head *bh = NULL;
  192. struct ext4_xattr_entry *entry;
  193. size_t size;
  194. int error;
  195. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  196. name_index, name, buffer, (long)buffer_size);
  197. error = -ENODATA;
  198. if (!EXT4_I(inode)->i_file_acl)
  199. goto cleanup;
  200. ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
  201. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  202. if (!bh)
  203. goto cleanup;
  204. ea_bdebug(bh, "b_count=%d, refcount=%d",
  205. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  206. if (ext4_xattr_check_block(bh)) {
  207. bad_block:
  208. EXT4_ERROR_INODE(inode, "bad block %llu",
  209. EXT4_I(inode)->i_file_acl);
  210. error = -EIO;
  211. goto cleanup;
  212. }
  213. ext4_xattr_cache_insert(bh);
  214. entry = BFIRST(bh);
  215. error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
  216. if (error == -EIO)
  217. goto bad_block;
  218. if (error)
  219. goto cleanup;
  220. size = le32_to_cpu(entry->e_value_size);
  221. if (buffer) {
  222. error = -ERANGE;
  223. if (size > buffer_size)
  224. goto cleanup;
  225. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  226. size);
  227. }
  228. error = size;
  229. cleanup:
  230. brelse(bh);
  231. return error;
  232. }
  233. static int
  234. ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  235. void *buffer, size_t buffer_size)
  236. {
  237. struct ext4_xattr_ibody_header *header;
  238. struct ext4_xattr_entry *entry;
  239. struct ext4_inode *raw_inode;
  240. struct ext4_iloc iloc;
  241. size_t size;
  242. void *end;
  243. int error;
  244. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  245. return -ENODATA;
  246. error = ext4_get_inode_loc(inode, &iloc);
  247. if (error)
  248. return error;
  249. raw_inode = ext4_raw_inode(&iloc);
  250. header = IHDR(inode, raw_inode);
  251. entry = IFIRST(header);
  252. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  253. error = ext4_xattr_check_names(entry, end);
  254. if (error)
  255. goto cleanup;
  256. error = ext4_xattr_find_entry(&entry, name_index, name,
  257. end - (void *)entry, 0);
  258. if (error)
  259. goto cleanup;
  260. size = le32_to_cpu(entry->e_value_size);
  261. if (buffer) {
  262. error = -ERANGE;
  263. if (size > buffer_size)
  264. goto cleanup;
  265. memcpy(buffer, (void *)IFIRST(header) +
  266. le16_to_cpu(entry->e_value_offs), size);
  267. }
  268. error = size;
  269. cleanup:
  270. brelse(iloc.bh);
  271. return error;
  272. }
  273. /*
  274. * ext4_xattr_get()
  275. *
  276. * Copy an extended attribute into the buffer
  277. * provided, or compute the buffer size required.
  278. * Buffer is NULL to compute the size of the buffer required.
  279. *
  280. * Returns a negative error number on failure, or the number of bytes
  281. * used / required on success.
  282. */
  283. int
  284. ext4_xattr_get(struct inode *inode, int name_index, const char *name,
  285. void *buffer, size_t buffer_size)
  286. {
  287. int error;
  288. down_read(&EXT4_I(inode)->xattr_sem);
  289. error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
  290. buffer_size);
  291. if (error == -ENODATA)
  292. error = ext4_xattr_block_get(inode, name_index, name, buffer,
  293. buffer_size);
  294. up_read(&EXT4_I(inode)->xattr_sem);
  295. return error;
  296. }
  297. static int
  298. ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
  299. char *buffer, size_t buffer_size)
  300. {
  301. size_t rest = buffer_size;
  302. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  303. const struct xattr_handler *handler =
  304. ext4_xattr_handler(entry->e_name_index);
  305. if (handler) {
  306. size_t size = handler->list(dentry, buffer, rest,
  307. entry->e_name,
  308. entry->e_name_len,
  309. handler->flags);
  310. if (buffer) {
  311. if (size > rest)
  312. return -ERANGE;
  313. buffer += size;
  314. }
  315. rest -= size;
  316. }
  317. }
  318. return buffer_size - rest;
  319. }
  320. static int
  321. ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  322. {
  323. struct inode *inode = dentry->d_inode;
  324. struct buffer_head *bh = NULL;
  325. int error;
  326. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  327. buffer, (long)buffer_size);
  328. error = 0;
  329. if (!EXT4_I(inode)->i_file_acl)
  330. goto cleanup;
  331. ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
  332. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  333. error = -EIO;
  334. if (!bh)
  335. goto cleanup;
  336. ea_bdebug(bh, "b_count=%d, refcount=%d",
  337. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  338. if (ext4_xattr_check_block(bh)) {
  339. EXT4_ERROR_INODE(inode, "bad block %llu",
  340. EXT4_I(inode)->i_file_acl);
  341. error = -EIO;
  342. goto cleanup;
  343. }
  344. ext4_xattr_cache_insert(bh);
  345. error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
  346. cleanup:
  347. brelse(bh);
  348. return error;
  349. }
  350. static int
  351. ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  352. {
  353. struct inode *inode = dentry->d_inode;
  354. struct ext4_xattr_ibody_header *header;
  355. struct ext4_inode *raw_inode;
  356. struct ext4_iloc iloc;
  357. void *end;
  358. int error;
  359. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  360. return 0;
  361. error = ext4_get_inode_loc(inode, &iloc);
  362. if (error)
  363. return error;
  364. raw_inode = ext4_raw_inode(&iloc);
  365. header = IHDR(inode, raw_inode);
  366. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  367. error = ext4_xattr_check_names(IFIRST(header), end);
  368. if (error)
  369. goto cleanup;
  370. error = ext4_xattr_list_entries(dentry, IFIRST(header),
  371. buffer, buffer_size);
  372. cleanup:
  373. brelse(iloc.bh);
  374. return error;
  375. }
  376. /*
  377. * ext4_xattr_list()
  378. *
  379. * Copy a list of attribute names into the buffer
  380. * provided, or compute the buffer size required.
  381. * Buffer is NULL to compute the size of the buffer required.
  382. *
  383. * Returns a negative error number on failure, or the number of bytes
  384. * used / required on success.
  385. */
  386. static int
  387. ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  388. {
  389. int ret, ret2;
  390. down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
  391. ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
  392. if (ret < 0)
  393. goto errout;
  394. if (buffer) {
  395. buffer += ret;
  396. buffer_size -= ret;
  397. }
  398. ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
  399. if (ret < 0)
  400. goto errout;
  401. ret += ret2;
  402. errout:
  403. up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
  404. return ret;
  405. }
  406. /*
  407. * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  408. * not set, set it.
  409. */
  410. static void ext4_xattr_update_super_block(handle_t *handle,
  411. struct super_block *sb)
  412. {
  413. if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
  414. return;
  415. if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
  416. EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
  417. ext4_handle_dirty_super(handle, sb);
  418. }
  419. }
  420. /*
  421. * Release the xattr block BH: If the reference count is > 1, decrement
  422. * it; otherwise free the block.
  423. */
  424. static void
  425. ext4_xattr_release_block(handle_t *handle, struct inode *inode,
  426. struct buffer_head *bh)
  427. {
  428. struct mb_cache_entry *ce = NULL;
  429. int error = 0;
  430. ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
  431. error = ext4_journal_get_write_access(handle, bh);
  432. if (error)
  433. goto out;
  434. lock_buffer(bh);
  435. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  436. ea_bdebug(bh, "refcount now=0; freeing");
  437. if (ce)
  438. mb_cache_entry_free(ce);
  439. get_bh(bh);
  440. ext4_free_blocks(handle, inode, bh, 0, 1,
  441. EXT4_FREE_BLOCKS_METADATA |
  442. EXT4_FREE_BLOCKS_FORGET);
  443. unlock_buffer(bh);
  444. } else {
  445. le32_add_cpu(&BHDR(bh)->h_refcount, -1);
  446. if (ce)
  447. mb_cache_entry_release(ce);
  448. unlock_buffer(bh);
  449. error = ext4_handle_dirty_metadata(handle, inode, bh);
  450. if (IS_SYNC(inode))
  451. ext4_handle_sync(handle);
  452. dquot_free_block(inode, 1);
  453. ea_bdebug(bh, "refcount now=%d; releasing",
  454. le32_to_cpu(BHDR(bh)->h_refcount));
  455. }
  456. out:
  457. ext4_std_error(inode->i_sb, error);
  458. return;
  459. }
  460. /*
  461. * Find the available free space for EAs. This also returns the total number of
  462. * bytes used by EA entries.
  463. */
  464. static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
  465. size_t *min_offs, void *base, int *total)
  466. {
  467. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  468. *total += EXT4_XATTR_LEN(last->e_name_len);
  469. if (!last->e_value_block && last->e_value_size) {
  470. size_t offs = le16_to_cpu(last->e_value_offs);
  471. if (offs < *min_offs)
  472. *min_offs = offs;
  473. }
  474. }
  475. return (*min_offs - ((void *)last - base) - sizeof(__u32));
  476. }
  477. struct ext4_xattr_info {
  478. int name_index;
  479. const char *name;
  480. const void *value;
  481. size_t value_len;
  482. };
  483. struct ext4_xattr_search {
  484. struct ext4_xattr_entry *first;
  485. void *base;
  486. void *end;
  487. struct ext4_xattr_entry *here;
  488. int not_found;
  489. };
  490. static int
  491. ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
  492. {
  493. struct ext4_xattr_entry *last;
  494. size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
  495. /* Compute min_offs and last. */
  496. last = s->first;
  497. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  498. if (!last->e_value_block && last->e_value_size) {
  499. size_t offs = le16_to_cpu(last->e_value_offs);
  500. if (offs < min_offs)
  501. min_offs = offs;
  502. }
  503. }
  504. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  505. if (!s->not_found) {
  506. if (!s->here->e_value_block && s->here->e_value_size) {
  507. size_t size = le32_to_cpu(s->here->e_value_size);
  508. free += EXT4_XATTR_SIZE(size);
  509. }
  510. free += EXT4_XATTR_LEN(name_len);
  511. }
  512. if (i->value) {
  513. if (free < EXT4_XATTR_SIZE(i->value_len) ||
  514. free < EXT4_XATTR_LEN(name_len) +
  515. EXT4_XATTR_SIZE(i->value_len))
  516. return -ENOSPC;
  517. }
  518. if (i->value && s->not_found) {
  519. /* Insert the new name. */
  520. size_t size = EXT4_XATTR_LEN(name_len);
  521. size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
  522. memmove((void *)s->here + size, s->here, rest);
  523. memset(s->here, 0, size);
  524. s->here->e_name_index = i->name_index;
  525. s->here->e_name_len = name_len;
  526. memcpy(s->here->e_name, i->name, name_len);
  527. } else {
  528. if (!s->here->e_value_block && s->here->e_value_size) {
  529. void *first_val = s->base + min_offs;
  530. size_t offs = le16_to_cpu(s->here->e_value_offs);
  531. void *val = s->base + offs;
  532. size_t size = EXT4_XATTR_SIZE(
  533. le32_to_cpu(s->here->e_value_size));
  534. if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
  535. /* The old and the new value have the same
  536. size. Just replace. */
  537. s->here->e_value_size =
  538. cpu_to_le32(i->value_len);
  539. memset(val + size - EXT4_XATTR_PAD, 0,
  540. EXT4_XATTR_PAD); /* Clear pad bytes. */
  541. memcpy(val, i->value, i->value_len);
  542. return 0;
  543. }
  544. /* Remove the old value. */
  545. memmove(first_val + size, first_val, val - first_val);
  546. memset(first_val, 0, size);
  547. s->here->e_value_size = 0;
  548. s->here->e_value_offs = 0;
  549. min_offs += size;
  550. /* Adjust all value offsets. */
  551. last = s->first;
  552. while (!IS_LAST_ENTRY(last)) {
  553. size_t o = le16_to_cpu(last->e_value_offs);
  554. if (!last->e_value_block &&
  555. last->e_value_size && o < offs)
  556. last->e_value_offs =
  557. cpu_to_le16(o + size);
  558. last = EXT4_XATTR_NEXT(last);
  559. }
  560. }
  561. if (!i->value) {
  562. /* Remove the old name. */
  563. size_t size = EXT4_XATTR_LEN(name_len);
  564. last = ENTRY((void *)last - size);
  565. memmove(s->here, (void *)s->here + size,
  566. (void *)last - (void *)s->here + sizeof(__u32));
  567. memset(last, 0, size);
  568. }
  569. }
  570. if (i->value) {
  571. /* Insert the new value. */
  572. s->here->e_value_size = cpu_to_le32(i->value_len);
  573. if (i->value_len) {
  574. size_t size = EXT4_XATTR_SIZE(i->value_len);
  575. void *val = s->base + min_offs - size;
  576. s->here->e_value_offs = cpu_to_le16(min_offs - size);
  577. memset(val + size - EXT4_XATTR_PAD, 0,
  578. EXT4_XATTR_PAD); /* Clear the pad bytes. */
  579. memcpy(val, i->value, i->value_len);
  580. }
  581. }
  582. return 0;
  583. }
  584. struct ext4_xattr_block_find {
  585. struct ext4_xattr_search s;
  586. struct buffer_head *bh;
  587. };
  588. static int
  589. ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
  590. struct ext4_xattr_block_find *bs)
  591. {
  592. struct super_block *sb = inode->i_sb;
  593. int error;
  594. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  595. i->name_index, i->name, i->value, (long)i->value_len);
  596. if (EXT4_I(inode)->i_file_acl) {
  597. /* The inode already has an extended attribute block. */
  598. bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
  599. error = -EIO;
  600. if (!bs->bh)
  601. goto cleanup;
  602. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  603. atomic_read(&(bs->bh->b_count)),
  604. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  605. if (ext4_xattr_check_block(bs->bh)) {
  606. EXT4_ERROR_INODE(inode, "bad block %llu",
  607. EXT4_I(inode)->i_file_acl);
  608. error = -EIO;
  609. goto cleanup;
  610. }
  611. /* Find the named attribute. */
  612. bs->s.base = BHDR(bs->bh);
  613. bs->s.first = BFIRST(bs->bh);
  614. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  615. bs->s.here = bs->s.first;
  616. error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
  617. i->name, bs->bh->b_size, 1);
  618. if (error && error != -ENODATA)
  619. goto cleanup;
  620. bs->s.not_found = error;
  621. }
  622. error = 0;
  623. cleanup:
  624. return error;
  625. }
  626. static int
  627. ext4_xattr_block_set(handle_t *handle, struct inode *inode,
  628. struct ext4_xattr_info *i,
  629. struct ext4_xattr_block_find *bs)
  630. {
  631. struct super_block *sb = inode->i_sb;
  632. struct buffer_head *new_bh = NULL;
  633. struct ext4_xattr_search *s = &bs->s;
  634. struct mb_cache_entry *ce = NULL;
  635. int error = 0;
  636. #define header(x) ((struct ext4_xattr_header *)(x))
  637. if (i->value && i->value_len > sb->s_blocksize)
  638. return -ENOSPC;
  639. if (s->base) {
  640. ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
  641. bs->bh->b_blocknr);
  642. error = ext4_journal_get_write_access(handle, bs->bh);
  643. if (error)
  644. goto cleanup;
  645. lock_buffer(bs->bh);
  646. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  647. if (ce) {
  648. mb_cache_entry_free(ce);
  649. ce = NULL;
  650. }
  651. ea_bdebug(bs->bh, "modifying in-place");
  652. error = ext4_xattr_set_entry(i, s);
  653. if (!error) {
  654. if (!IS_LAST_ENTRY(s->first))
  655. ext4_xattr_rehash(header(s->base),
  656. s->here);
  657. ext4_xattr_cache_insert(bs->bh);
  658. }
  659. unlock_buffer(bs->bh);
  660. if (error == -EIO)
  661. goto bad_block;
  662. if (!error)
  663. error = ext4_handle_dirty_metadata(handle,
  664. inode,
  665. bs->bh);
  666. if (error)
  667. goto cleanup;
  668. goto inserted;
  669. } else {
  670. int offset = (char *)s->here - bs->bh->b_data;
  671. unlock_buffer(bs->bh);
  672. ext4_handle_release_buffer(handle, bs->bh);
  673. if (ce) {
  674. mb_cache_entry_release(ce);
  675. ce = NULL;
  676. }
  677. ea_bdebug(bs->bh, "cloning");
  678. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  679. error = -ENOMEM;
  680. if (s->base == NULL)
  681. goto cleanup;
  682. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  683. s->first = ENTRY(header(s->base)+1);
  684. header(s->base)->h_refcount = cpu_to_le32(1);
  685. s->here = ENTRY(s->base + offset);
  686. s->end = s->base + bs->bh->b_size;
  687. }
  688. } else {
  689. /* Allocate a buffer where we construct the new block. */
  690. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  691. /* assert(header == s->base) */
  692. error = -ENOMEM;
  693. if (s->base == NULL)
  694. goto cleanup;
  695. header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  696. header(s->base)->h_blocks = cpu_to_le32(1);
  697. header(s->base)->h_refcount = cpu_to_le32(1);
  698. s->first = ENTRY(header(s->base)+1);
  699. s->here = ENTRY(header(s->base)+1);
  700. s->end = s->base + sb->s_blocksize;
  701. }
  702. error = ext4_xattr_set_entry(i, s);
  703. if (error == -EIO)
  704. goto bad_block;
  705. if (error)
  706. goto cleanup;
  707. if (!IS_LAST_ENTRY(s->first))
  708. ext4_xattr_rehash(header(s->base), s->here);
  709. inserted:
  710. if (!IS_LAST_ENTRY(s->first)) {
  711. new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
  712. if (new_bh) {
  713. /* We found an identical block in the cache. */
  714. if (new_bh == bs->bh)
  715. ea_bdebug(new_bh, "keeping");
  716. else {
  717. /* The old block is released after updating
  718. the inode. */
  719. error = dquot_alloc_block(inode, 1);
  720. if (error)
  721. goto cleanup;
  722. error = ext4_journal_get_write_access(handle,
  723. new_bh);
  724. if (error)
  725. goto cleanup_dquot;
  726. lock_buffer(new_bh);
  727. le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
  728. ea_bdebug(new_bh, "reusing; refcount now=%d",
  729. le32_to_cpu(BHDR(new_bh)->h_refcount));
  730. unlock_buffer(new_bh);
  731. error = ext4_handle_dirty_metadata(handle,
  732. inode,
  733. new_bh);
  734. if (error)
  735. goto cleanup_dquot;
  736. }
  737. mb_cache_entry_release(ce);
  738. ce = NULL;
  739. } else if (bs->bh && s->base == bs->bh->b_data) {
  740. /* We were modifying this block in-place. */
  741. ea_bdebug(bs->bh, "keeping this block");
  742. new_bh = bs->bh;
  743. get_bh(new_bh);
  744. } else {
  745. /* We need to allocate a new block */
  746. ext4_fsblk_t goal, block;
  747. goal = ext4_group_first_block_no(sb,
  748. EXT4_I(inode)->i_block_group);
  749. /* non-extent files can't have physical blocks past 2^32 */
  750. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  751. goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
  752. /*
  753. * take i_data_sem because we will test
  754. * i_delalloc_reserved_flag in ext4_mb_new_blocks
  755. */
  756. down_read((&EXT4_I(inode)->i_data_sem));
  757. block = ext4_new_meta_blocks(handle, inode, goal, 0,
  758. NULL, &error);
  759. up_read((&EXT4_I(inode)->i_data_sem));
  760. if (error)
  761. goto cleanup;
  762. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  763. BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
  764. ea_idebug(inode, "creating block %d", block);
  765. new_bh = sb_getblk(sb, block);
  766. if (!new_bh) {
  767. getblk_failed:
  768. ext4_free_blocks(handle, inode, NULL, block, 1,
  769. EXT4_FREE_BLOCKS_METADATA);
  770. error = -EIO;
  771. goto cleanup;
  772. }
  773. lock_buffer(new_bh);
  774. error = ext4_journal_get_create_access(handle, new_bh);
  775. if (error) {
  776. unlock_buffer(new_bh);
  777. goto getblk_failed;
  778. }
  779. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  780. set_buffer_uptodate(new_bh);
  781. unlock_buffer(new_bh);
  782. ext4_xattr_cache_insert(new_bh);
  783. error = ext4_handle_dirty_metadata(handle,
  784. inode, new_bh);
  785. if (error)
  786. goto cleanup;
  787. }
  788. }
  789. /* Update the inode. */
  790. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  791. /* Drop the previous xattr block. */
  792. if (bs->bh && bs->bh != new_bh)
  793. ext4_xattr_release_block(handle, inode, bs->bh);
  794. error = 0;
  795. cleanup:
  796. if (ce)
  797. mb_cache_entry_release(ce);
  798. brelse(new_bh);
  799. if (!(bs->bh && s->base == bs->bh->b_data))
  800. kfree(s->base);
  801. return error;
  802. cleanup_dquot:
  803. dquot_free_block(inode, 1);
  804. goto cleanup;
  805. bad_block:
  806. EXT4_ERROR_INODE(inode, "bad block %llu",
  807. EXT4_I(inode)->i_file_acl);
  808. goto cleanup;
  809. #undef header
  810. }
  811. struct ext4_xattr_ibody_find {
  812. struct ext4_xattr_search s;
  813. struct ext4_iloc iloc;
  814. };
  815. static int
  816. ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  817. struct ext4_xattr_ibody_find *is)
  818. {
  819. struct ext4_xattr_ibody_header *header;
  820. struct ext4_inode *raw_inode;
  821. int error;
  822. if (EXT4_I(inode)->i_extra_isize == 0)
  823. return 0;
  824. raw_inode = ext4_raw_inode(&is->iloc);
  825. header = IHDR(inode, raw_inode);
  826. is->s.base = is->s.first = IFIRST(header);
  827. is->s.here = is->s.first;
  828. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  829. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  830. error = ext4_xattr_check_names(IFIRST(header), is->s.end);
  831. if (error)
  832. return error;
  833. /* Find the named attribute. */
  834. error = ext4_xattr_find_entry(&is->s.here, i->name_index,
  835. i->name, is->s.end -
  836. (void *)is->s.base, 0);
  837. if (error && error != -ENODATA)
  838. return error;
  839. is->s.not_found = error;
  840. }
  841. return 0;
  842. }
  843. static int
  844. ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  845. struct ext4_xattr_info *i,
  846. struct ext4_xattr_ibody_find *is)
  847. {
  848. struct ext4_xattr_ibody_header *header;
  849. struct ext4_xattr_search *s = &is->s;
  850. int error;
  851. if (EXT4_I(inode)->i_extra_isize == 0)
  852. return -ENOSPC;
  853. error = ext4_xattr_set_entry(i, s);
  854. if (error)
  855. return error;
  856. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  857. if (!IS_LAST_ENTRY(s->first)) {
  858. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  859. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  860. } else {
  861. header->h_magic = cpu_to_le32(0);
  862. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  863. }
  864. return 0;
  865. }
  866. /*
  867. * ext4_xattr_set_handle()
  868. *
  869. * Create, replace or remove an extended attribute for this inode. Value
  870. * is NULL to remove an existing extended attribute, and non-NULL to
  871. * either replace an existing extended attribute, or create a new extended
  872. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  873. * specify that an extended attribute must exist and must not exist
  874. * previous to the call, respectively.
  875. *
  876. * Returns 0, or a negative error number on failure.
  877. */
  878. int
  879. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  880. const char *name, const void *value, size_t value_len,
  881. int flags)
  882. {
  883. struct ext4_xattr_info i = {
  884. .name_index = name_index,
  885. .name = name,
  886. .value = value,
  887. .value_len = value_len,
  888. };
  889. struct ext4_xattr_ibody_find is = {
  890. .s = { .not_found = -ENODATA, },
  891. };
  892. struct ext4_xattr_block_find bs = {
  893. .s = { .not_found = -ENODATA, },
  894. };
  895. unsigned long no_expand;
  896. int error;
  897. if (!name)
  898. return -EINVAL;
  899. if (strlen(name) > 255)
  900. return -ERANGE;
  901. down_write(&EXT4_I(inode)->xattr_sem);
  902. no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
  903. ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
  904. error = ext4_reserve_inode_write(handle, inode, &is.iloc);
  905. if (error)
  906. goto cleanup;
  907. if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
  908. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  909. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  910. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  911. }
  912. error = ext4_xattr_ibody_find(inode, &i, &is);
  913. if (error)
  914. goto cleanup;
  915. if (is.s.not_found)
  916. error = ext4_xattr_block_find(inode, &i, &bs);
  917. if (error)
  918. goto cleanup;
  919. if (is.s.not_found && bs.s.not_found) {
  920. error = -ENODATA;
  921. if (flags & XATTR_REPLACE)
  922. goto cleanup;
  923. error = 0;
  924. if (!value)
  925. goto cleanup;
  926. } else {
  927. error = -EEXIST;
  928. if (flags & XATTR_CREATE)
  929. goto cleanup;
  930. }
  931. if (!value) {
  932. if (!is.s.not_found)
  933. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  934. else if (!bs.s.not_found)
  935. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  936. } else {
  937. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  938. if (!error && !bs.s.not_found) {
  939. i.value = NULL;
  940. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  941. } else if (error == -ENOSPC) {
  942. if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
  943. error = ext4_xattr_block_find(inode, &i, &bs);
  944. if (error)
  945. goto cleanup;
  946. }
  947. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  948. if (error)
  949. goto cleanup;
  950. if (!is.s.not_found) {
  951. i.value = NULL;
  952. error = ext4_xattr_ibody_set(handle, inode, &i,
  953. &is);
  954. }
  955. }
  956. }
  957. if (!error) {
  958. ext4_xattr_update_super_block(handle, inode->i_sb);
  959. inode->i_ctime = ext4_current_time(inode);
  960. if (!value)
  961. ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
  962. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  963. /*
  964. * The bh is consumed by ext4_mark_iloc_dirty, even with
  965. * error != 0.
  966. */
  967. is.iloc.bh = NULL;
  968. if (IS_SYNC(inode))
  969. ext4_handle_sync(handle);
  970. }
  971. cleanup:
  972. brelse(is.iloc.bh);
  973. brelse(bs.bh);
  974. if (no_expand == 0)
  975. ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
  976. up_write(&EXT4_I(inode)->xattr_sem);
  977. return error;
  978. }
  979. /*
  980. * ext4_xattr_set()
  981. *
  982. * Like ext4_xattr_set_handle, but start from an inode. This extended
  983. * attribute modification is a filesystem transaction by itself.
  984. *
  985. * Returns 0, or a negative error number on failure.
  986. */
  987. int
  988. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  989. const void *value, size_t value_len, int flags)
  990. {
  991. handle_t *handle;
  992. int error, retries = 0;
  993. retry:
  994. handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
  995. if (IS_ERR(handle)) {
  996. error = PTR_ERR(handle);
  997. } else {
  998. int error2;
  999. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  1000. value, value_len, flags);
  1001. error2 = ext4_journal_stop(handle);
  1002. if (error == -ENOSPC &&
  1003. ext4_should_retry_alloc(inode->i_sb, &retries))
  1004. goto retry;
  1005. if (error == 0)
  1006. error = error2;
  1007. }
  1008. return error;
  1009. }
  1010. /*
  1011. * Shift the EA entries in the inode to create space for the increased
  1012. * i_extra_isize.
  1013. */
  1014. static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
  1015. int value_offs_shift, void *to,
  1016. void *from, size_t n, int blocksize)
  1017. {
  1018. struct ext4_xattr_entry *last = entry;
  1019. int new_offs;
  1020. /* Adjust the value offsets of the entries */
  1021. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1022. if (!last->e_value_block && last->e_value_size) {
  1023. new_offs = le16_to_cpu(last->e_value_offs) +
  1024. value_offs_shift;
  1025. BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
  1026. > blocksize);
  1027. last->e_value_offs = cpu_to_le16(new_offs);
  1028. }
  1029. }
  1030. /* Shift the entries by n bytes */
  1031. memmove(to, from, n);
  1032. }
  1033. /*
  1034. * Expand an inode by new_extra_isize bytes when EAs are present.
  1035. * Returns 0 on success or negative error number on failure.
  1036. */
  1037. int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
  1038. struct ext4_inode *raw_inode, handle_t *handle)
  1039. {
  1040. struct ext4_xattr_ibody_header *header;
  1041. struct ext4_xattr_entry *entry, *last, *first;
  1042. struct buffer_head *bh = NULL;
  1043. struct ext4_xattr_ibody_find *is = NULL;
  1044. struct ext4_xattr_block_find *bs = NULL;
  1045. char *buffer = NULL, *b_entry_name = NULL;
  1046. size_t min_offs, free;
  1047. int total_ino, total_blk;
  1048. void *base, *start, *end;
  1049. int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
  1050. int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
  1051. down_write(&EXT4_I(inode)->xattr_sem);
  1052. retry:
  1053. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
  1054. up_write(&EXT4_I(inode)->xattr_sem);
  1055. return 0;
  1056. }
  1057. header = IHDR(inode, raw_inode);
  1058. entry = IFIRST(header);
  1059. /*
  1060. * Check if enough free space is available in the inode to shift the
  1061. * entries ahead by new_extra_isize.
  1062. */
  1063. base = start = entry;
  1064. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1065. min_offs = end - base;
  1066. last = entry;
  1067. total_ino = sizeof(struct ext4_xattr_ibody_header);
  1068. free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
  1069. if (free >= new_extra_isize) {
  1070. entry = IFIRST(header);
  1071. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
  1072. - new_extra_isize, (void *)raw_inode +
  1073. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  1074. (void *)header, total_ino,
  1075. inode->i_sb->s_blocksize);
  1076. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  1077. error = 0;
  1078. goto cleanup;
  1079. }
  1080. /*
  1081. * Enough free space isn't available in the inode, check if
  1082. * EA block can hold new_extra_isize bytes.
  1083. */
  1084. if (EXT4_I(inode)->i_file_acl) {
  1085. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1086. error = -EIO;
  1087. if (!bh)
  1088. goto cleanup;
  1089. if (ext4_xattr_check_block(bh)) {
  1090. EXT4_ERROR_INODE(inode, "bad block %llu",
  1091. EXT4_I(inode)->i_file_acl);
  1092. error = -EIO;
  1093. goto cleanup;
  1094. }
  1095. base = BHDR(bh);
  1096. first = BFIRST(bh);
  1097. end = bh->b_data + bh->b_size;
  1098. min_offs = end - base;
  1099. free = ext4_xattr_free_space(first, &min_offs, base,
  1100. &total_blk);
  1101. if (free < new_extra_isize) {
  1102. if (!tried_min_extra_isize && s_min_extra_isize) {
  1103. tried_min_extra_isize++;
  1104. new_extra_isize = s_min_extra_isize;
  1105. brelse(bh);
  1106. goto retry;
  1107. }
  1108. error = -1;
  1109. goto cleanup;
  1110. }
  1111. } else {
  1112. free = inode->i_sb->s_blocksize;
  1113. }
  1114. while (new_extra_isize > 0) {
  1115. size_t offs, size, entry_size;
  1116. struct ext4_xattr_entry *small_entry = NULL;
  1117. struct ext4_xattr_info i = {
  1118. .value = NULL,
  1119. .value_len = 0,
  1120. };
  1121. unsigned int total_size; /* EA entry size + value size */
  1122. unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
  1123. unsigned int min_total_size = ~0U;
  1124. is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
  1125. bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
  1126. if (!is || !bs) {
  1127. error = -ENOMEM;
  1128. goto cleanup;
  1129. }
  1130. is->s.not_found = -ENODATA;
  1131. bs->s.not_found = -ENODATA;
  1132. is->iloc.bh = NULL;
  1133. bs->bh = NULL;
  1134. last = IFIRST(header);
  1135. /* Find the entry best suited to be pushed into EA block */
  1136. entry = NULL;
  1137. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1138. total_size =
  1139. EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
  1140. EXT4_XATTR_LEN(last->e_name_len);
  1141. if (total_size <= free && total_size < min_total_size) {
  1142. if (total_size < new_extra_isize) {
  1143. small_entry = last;
  1144. } else {
  1145. entry = last;
  1146. min_total_size = total_size;
  1147. }
  1148. }
  1149. }
  1150. if (entry == NULL) {
  1151. if (small_entry) {
  1152. entry = small_entry;
  1153. } else {
  1154. if (!tried_min_extra_isize &&
  1155. s_min_extra_isize) {
  1156. tried_min_extra_isize++;
  1157. new_extra_isize = s_min_extra_isize;
  1158. goto retry;
  1159. }
  1160. error = -1;
  1161. goto cleanup;
  1162. }
  1163. }
  1164. offs = le16_to_cpu(entry->e_value_offs);
  1165. size = le32_to_cpu(entry->e_value_size);
  1166. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  1167. i.name_index = entry->e_name_index,
  1168. buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
  1169. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
  1170. if (!buffer || !b_entry_name) {
  1171. error = -ENOMEM;
  1172. goto cleanup;
  1173. }
  1174. /* Save the entry name and the entry value */
  1175. memcpy(buffer, (void *)IFIRST(header) + offs,
  1176. EXT4_XATTR_SIZE(size));
  1177. memcpy(b_entry_name, entry->e_name, entry->e_name_len);
  1178. b_entry_name[entry->e_name_len] = '\0';
  1179. i.name = b_entry_name;
  1180. error = ext4_get_inode_loc(inode, &is->iloc);
  1181. if (error)
  1182. goto cleanup;
  1183. error = ext4_xattr_ibody_find(inode, &i, is);
  1184. if (error)
  1185. goto cleanup;
  1186. /* Remove the chosen entry from the inode */
  1187. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  1188. if (error)
  1189. goto cleanup;
  1190. entry = IFIRST(header);
  1191. if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
  1192. shift_bytes = new_extra_isize;
  1193. else
  1194. shift_bytes = entry_size + size;
  1195. /* Adjust the offsets and shift the remaining entries ahead */
  1196. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
  1197. shift_bytes, (void *)raw_inode +
  1198. EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
  1199. (void *)header, total_ino - entry_size,
  1200. inode->i_sb->s_blocksize);
  1201. extra_isize += shift_bytes;
  1202. new_extra_isize -= shift_bytes;
  1203. EXT4_I(inode)->i_extra_isize = extra_isize;
  1204. i.name = b_entry_name;
  1205. i.value = buffer;
  1206. i.value_len = size;
  1207. error = ext4_xattr_block_find(inode, &i, bs);
  1208. if (error)
  1209. goto cleanup;
  1210. /* Add entry which was removed from the inode into the block */
  1211. error = ext4_xattr_block_set(handle, inode, &i, bs);
  1212. if (error)
  1213. goto cleanup;
  1214. kfree(b_entry_name);
  1215. kfree(buffer);
  1216. b_entry_name = NULL;
  1217. buffer = NULL;
  1218. brelse(is->iloc.bh);
  1219. kfree(is);
  1220. kfree(bs);
  1221. }
  1222. brelse(bh);
  1223. up_write(&EXT4_I(inode)->xattr_sem);
  1224. return 0;
  1225. cleanup:
  1226. kfree(b_entry_name);
  1227. kfree(buffer);
  1228. if (is)
  1229. brelse(is->iloc.bh);
  1230. kfree(is);
  1231. kfree(bs);
  1232. brelse(bh);
  1233. up_write(&EXT4_I(inode)->xattr_sem);
  1234. return error;
  1235. }
  1236. /*
  1237. * ext4_xattr_delete_inode()
  1238. *
  1239. * Free extended attribute resources associated with this inode. This
  1240. * is called immediately before an inode is freed. We have exclusive
  1241. * access to the inode.
  1242. */
  1243. void
  1244. ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
  1245. {
  1246. struct buffer_head *bh = NULL;
  1247. if (!EXT4_I(inode)->i_file_acl)
  1248. goto cleanup;
  1249. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1250. if (!bh) {
  1251. EXT4_ERROR_INODE(inode, "block %llu read error",
  1252. EXT4_I(inode)->i_file_acl);
  1253. goto cleanup;
  1254. }
  1255. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  1256. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1257. EXT4_ERROR_INODE(inode, "bad block %llu",
  1258. EXT4_I(inode)->i_file_acl);
  1259. goto cleanup;
  1260. }
  1261. ext4_xattr_release_block(handle, inode, bh);
  1262. EXT4_I(inode)->i_file_acl = 0;
  1263. cleanup:
  1264. brelse(bh);
  1265. }
  1266. /*
  1267. * ext4_xattr_put_super()
  1268. *
  1269. * This is called when a file system is unmounted.
  1270. */
  1271. void
  1272. ext4_xattr_put_super(struct super_block *sb)
  1273. {
  1274. mb_cache_shrink(sb->s_bdev);
  1275. }
  1276. /*
  1277. * ext4_xattr_cache_insert()
  1278. *
  1279. * Create a new entry in the extended attribute cache, and insert
  1280. * it unless such an entry is already in the cache.
  1281. *
  1282. * Returns 0, or a negative error number on failure.
  1283. */
  1284. static void
  1285. ext4_xattr_cache_insert(struct buffer_head *bh)
  1286. {
  1287. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1288. struct mb_cache_entry *ce;
  1289. int error;
  1290. ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
  1291. if (!ce) {
  1292. ea_bdebug(bh, "out of memory");
  1293. return;
  1294. }
  1295. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
  1296. if (error) {
  1297. mb_cache_entry_free(ce);
  1298. if (error == -EBUSY) {
  1299. ea_bdebug(bh, "already in cache");
  1300. error = 0;
  1301. }
  1302. } else {
  1303. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1304. mb_cache_entry_release(ce);
  1305. }
  1306. }
  1307. /*
  1308. * ext4_xattr_cmp()
  1309. *
  1310. * Compare two extended attribute blocks for equality.
  1311. *
  1312. * Returns 0 if the blocks are equal, 1 if they differ, and
  1313. * a negative error number on errors.
  1314. */
  1315. static int
  1316. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  1317. struct ext4_xattr_header *header2)
  1318. {
  1319. struct ext4_xattr_entry *entry1, *entry2;
  1320. entry1 = ENTRY(header1+1);
  1321. entry2 = ENTRY(header2+1);
  1322. while (!IS_LAST_ENTRY(entry1)) {
  1323. if (IS_LAST_ENTRY(entry2))
  1324. return 1;
  1325. if (entry1->e_hash != entry2->e_hash ||
  1326. entry1->e_name_index != entry2->e_name_index ||
  1327. entry1->e_name_len != entry2->e_name_len ||
  1328. entry1->e_value_size != entry2->e_value_size ||
  1329. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1330. return 1;
  1331. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1332. return -EIO;
  1333. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1334. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1335. le32_to_cpu(entry1->e_value_size)))
  1336. return 1;
  1337. entry1 = EXT4_XATTR_NEXT(entry1);
  1338. entry2 = EXT4_XATTR_NEXT(entry2);
  1339. }
  1340. if (!IS_LAST_ENTRY(entry2))
  1341. return 1;
  1342. return 0;
  1343. }
  1344. /*
  1345. * ext4_xattr_cache_find()
  1346. *
  1347. * Find an identical extended attribute block.
  1348. *
  1349. * Returns a pointer to the block found, or NULL if such a block was
  1350. * not found or an error occurred.
  1351. */
  1352. static struct buffer_head *
  1353. ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
  1354. struct mb_cache_entry **pce)
  1355. {
  1356. __u32 hash = le32_to_cpu(header->h_hash);
  1357. struct mb_cache_entry *ce;
  1358. if (!header->h_hash)
  1359. return NULL; /* never share */
  1360. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1361. again:
  1362. ce = mb_cache_entry_find_first(ext4_xattr_cache, inode->i_sb->s_bdev,
  1363. hash);
  1364. while (ce) {
  1365. struct buffer_head *bh;
  1366. if (IS_ERR(ce)) {
  1367. if (PTR_ERR(ce) == -EAGAIN)
  1368. goto again;
  1369. break;
  1370. }
  1371. bh = sb_bread(inode->i_sb, ce->e_block);
  1372. if (!bh) {
  1373. EXT4_ERROR_INODE(inode, "block %lu read error",
  1374. (unsigned long) ce->e_block);
  1375. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1376. EXT4_XATTR_REFCOUNT_MAX) {
  1377. ea_idebug(inode, "block %lu refcount %d>=%d",
  1378. (unsigned long) ce->e_block,
  1379. le32_to_cpu(BHDR(bh)->h_refcount),
  1380. EXT4_XATTR_REFCOUNT_MAX);
  1381. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  1382. *pce = ce;
  1383. return bh;
  1384. }
  1385. brelse(bh);
  1386. ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
  1387. }
  1388. return NULL;
  1389. }
  1390. #define NAME_HASH_SHIFT 5
  1391. #define VALUE_HASH_SHIFT 16
  1392. /*
  1393. * ext4_xattr_hash_entry()
  1394. *
  1395. * Compute the hash of an extended attribute.
  1396. */
  1397. static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
  1398. struct ext4_xattr_entry *entry)
  1399. {
  1400. __u32 hash = 0;
  1401. char *name = entry->e_name;
  1402. int n;
  1403. for (n = 0; n < entry->e_name_len; n++) {
  1404. hash = (hash << NAME_HASH_SHIFT) ^
  1405. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1406. *name++;
  1407. }
  1408. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1409. __le32 *value = (__le32 *)((char *)header +
  1410. le16_to_cpu(entry->e_value_offs));
  1411. for (n = (le32_to_cpu(entry->e_value_size) +
  1412. EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
  1413. hash = (hash << VALUE_HASH_SHIFT) ^
  1414. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1415. le32_to_cpu(*value++);
  1416. }
  1417. }
  1418. entry->e_hash = cpu_to_le32(hash);
  1419. }
  1420. #undef NAME_HASH_SHIFT
  1421. #undef VALUE_HASH_SHIFT
  1422. #define BLOCK_HASH_SHIFT 16
  1423. /*
  1424. * ext4_xattr_rehash()
  1425. *
  1426. * Re-compute the extended attribute hash value after an entry has changed.
  1427. */
  1428. static void ext4_xattr_rehash(struct ext4_xattr_header *header,
  1429. struct ext4_xattr_entry *entry)
  1430. {
  1431. struct ext4_xattr_entry *here;
  1432. __u32 hash = 0;
  1433. ext4_xattr_hash_entry(header, entry);
  1434. here = ENTRY(header+1);
  1435. while (!IS_LAST_ENTRY(here)) {
  1436. if (!here->e_hash) {
  1437. /* Block is not shared if an entry's hash value == 0 */
  1438. hash = 0;
  1439. break;
  1440. }
  1441. hash = (hash << BLOCK_HASH_SHIFT) ^
  1442. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1443. le32_to_cpu(here->e_hash);
  1444. here = EXT4_XATTR_NEXT(here);
  1445. }
  1446. header->h_hash = cpu_to_le32(hash);
  1447. }
  1448. #undef BLOCK_HASH_SHIFT
  1449. int __init
  1450. ext4_init_xattr(void)
  1451. {
  1452. ext4_xattr_cache = mb_cache_create("ext4_xattr", 6);
  1453. if (!ext4_xattr_cache)
  1454. return -ENOMEM;
  1455. return 0;
  1456. }
  1457. void
  1458. ext4_exit_xattr(void)
  1459. {
  1460. if (ext4_xattr_cache)
  1461. mb_cache_destroy(ext4_xattr_cache);
  1462. ext4_xattr_cache = NULL;
  1463. }