xattr.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  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/ext4_jbd2.h>
  55. #include <linux/ext4_fs.h>
  56. #include <linux/mbcache.h>
  57. #include <linux/quotaops.h>
  58. #include <linux/rwsem.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 struct mb_cache *ext4_xattr_cache;
  91. static struct xattr_handler *ext4_xattr_handler_map[] = {
  92. [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
  93. #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
  94. [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext4_xattr_acl_access_handler,
  95. [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler,
  96. #endif
  97. [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
  98. #ifdef CONFIG_EXT4DEV_FS_SECURITY
  99. [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
  100. #endif
  101. };
  102. struct xattr_handler *ext4_xattr_handlers[] = {
  103. &ext4_xattr_user_handler,
  104. &ext4_xattr_trusted_handler,
  105. #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
  106. &ext4_xattr_acl_access_handler,
  107. &ext4_xattr_acl_default_handler,
  108. #endif
  109. #ifdef CONFIG_EXT4DEV_FS_SECURITY
  110. &ext4_xattr_security_handler,
  111. #endif
  112. NULL
  113. };
  114. static inline struct xattr_handler *
  115. ext4_xattr_handler(int name_index)
  116. {
  117. struct xattr_handler *handler = NULL;
  118. if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
  119. handler = ext4_xattr_handler_map[name_index];
  120. return handler;
  121. }
  122. /*
  123. * Inode operation listxattr()
  124. *
  125. * dentry->d_inode->i_mutex: don't care
  126. */
  127. ssize_t
  128. ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
  129. {
  130. return ext4_xattr_list(dentry->d_inode, buffer, size);
  131. }
  132. static int
  133. ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
  134. {
  135. while (!IS_LAST_ENTRY(entry)) {
  136. struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
  137. if ((void *)next >= end)
  138. return -EIO;
  139. entry = next;
  140. }
  141. return 0;
  142. }
  143. static inline int
  144. ext4_xattr_check_block(struct buffer_head *bh)
  145. {
  146. int error;
  147. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  148. BHDR(bh)->h_blocks != cpu_to_le32(1))
  149. return -EIO;
  150. error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
  151. return error;
  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: ext4_error(inode->i_sb, __FUNCTION__,
  208. "inode %lu: bad block %llu", inode->i_ino,
  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_I(inode)->i_state & 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 inode *inode, 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. struct xattr_handler *handler =
  304. ext4_xattr_handler(entry->e_name_index);
  305. if (handler) {
  306. size_t size = handler->list(inode, buffer, rest,
  307. entry->e_name,
  308. entry->e_name_len);
  309. if (buffer) {
  310. if (size > rest)
  311. return -ERANGE;
  312. buffer += size;
  313. }
  314. rest -= size;
  315. }
  316. }
  317. return buffer_size - rest;
  318. }
  319. static int
  320. ext4_xattr_block_list(struct inode *inode, char *buffer, size_t buffer_size)
  321. {
  322. struct buffer_head *bh = NULL;
  323. int error;
  324. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  325. buffer, (long)buffer_size);
  326. error = 0;
  327. if (!EXT4_I(inode)->i_file_acl)
  328. goto cleanup;
  329. ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
  330. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  331. error = -EIO;
  332. if (!bh)
  333. goto cleanup;
  334. ea_bdebug(bh, "b_count=%d, refcount=%d",
  335. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  336. if (ext4_xattr_check_block(bh)) {
  337. ext4_error(inode->i_sb, __FUNCTION__,
  338. "inode %lu: bad block %llu", inode->i_ino,
  339. EXT4_I(inode)->i_file_acl);
  340. error = -EIO;
  341. goto cleanup;
  342. }
  343. ext4_xattr_cache_insert(bh);
  344. error = ext4_xattr_list_entries(inode, BFIRST(bh), buffer, buffer_size);
  345. cleanup:
  346. brelse(bh);
  347. return error;
  348. }
  349. static int
  350. ext4_xattr_ibody_list(struct inode *inode, char *buffer, size_t buffer_size)
  351. {
  352. struct ext4_xattr_ibody_header *header;
  353. struct ext4_inode *raw_inode;
  354. struct ext4_iloc iloc;
  355. void *end;
  356. int error;
  357. if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR))
  358. return 0;
  359. error = ext4_get_inode_loc(inode, &iloc);
  360. if (error)
  361. return error;
  362. raw_inode = ext4_raw_inode(&iloc);
  363. header = IHDR(inode, raw_inode);
  364. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  365. error = ext4_xattr_check_names(IFIRST(header), end);
  366. if (error)
  367. goto cleanup;
  368. error = ext4_xattr_list_entries(inode, IFIRST(header),
  369. buffer, buffer_size);
  370. cleanup:
  371. brelse(iloc.bh);
  372. return error;
  373. }
  374. /*
  375. * ext4_xattr_list()
  376. *
  377. * Copy a list of attribute names into the buffer
  378. * provided, or compute the buffer size required.
  379. * Buffer is NULL to compute the size of the buffer required.
  380. *
  381. * Returns a negative error number on failure, or the number of bytes
  382. * used / required on success.
  383. */
  384. int
  385. ext4_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
  386. {
  387. int i_error, b_error;
  388. down_read(&EXT4_I(inode)->xattr_sem);
  389. i_error = ext4_xattr_ibody_list(inode, buffer, buffer_size);
  390. if (i_error < 0) {
  391. b_error = 0;
  392. } else {
  393. if (buffer) {
  394. buffer += i_error;
  395. buffer_size -= i_error;
  396. }
  397. b_error = ext4_xattr_block_list(inode, buffer, buffer_size);
  398. if (b_error < 0)
  399. i_error = 0;
  400. }
  401. up_read(&EXT4_I(inode)->xattr_sem);
  402. return i_error + b_error;
  403. }
  404. /*
  405. * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  406. * not set, set it.
  407. */
  408. static void ext4_xattr_update_super_block(handle_t *handle,
  409. struct super_block *sb)
  410. {
  411. if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
  412. return;
  413. if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
  414. EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
  415. sb->s_dirt = 1;
  416. ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
  417. }
  418. }
  419. /*
  420. * Release the xattr block BH: If the reference count is > 1, decrement
  421. * it; otherwise free the block.
  422. */
  423. static void
  424. ext4_xattr_release_block(handle_t *handle, struct inode *inode,
  425. struct buffer_head *bh)
  426. {
  427. struct mb_cache_entry *ce = NULL;
  428. int error = 0;
  429. ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
  430. error = ext4_journal_get_write_access(handle, bh);
  431. if (error)
  432. goto out;
  433. lock_buffer(bh);
  434. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  435. ea_bdebug(bh, "refcount now=0; freeing");
  436. if (ce)
  437. mb_cache_entry_free(ce);
  438. ext4_free_blocks(handle, inode, bh->b_blocknr, 1, 1);
  439. get_bh(bh);
  440. ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
  441. } else {
  442. le32_add_cpu(&BHDR(bh)->h_refcount, -1);
  443. error = ext4_journal_dirty_metadata(handle, bh);
  444. if (IS_SYNC(inode))
  445. handle->h_sync = 1;
  446. DQUOT_FREE_BLOCK(inode, 1);
  447. ea_bdebug(bh, "refcount now=%d; releasing",
  448. le32_to_cpu(BHDR(bh)->h_refcount));
  449. if (ce)
  450. mb_cache_entry_release(ce);
  451. }
  452. unlock_buffer(bh);
  453. out:
  454. ext4_std_error(inode->i_sb, error);
  455. return;
  456. }
  457. /*
  458. * Find the available free space for EAs. This also returns the total number of
  459. * bytes used by EA entries.
  460. */
  461. static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
  462. size_t *min_offs, void *base, int *total)
  463. {
  464. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  465. *total += EXT4_XATTR_LEN(last->e_name_len);
  466. if (!last->e_value_block && last->e_value_size) {
  467. size_t offs = le16_to_cpu(last->e_value_offs);
  468. if (offs < *min_offs)
  469. *min_offs = offs;
  470. }
  471. }
  472. return (*min_offs - ((void *)last - base) - sizeof(__u32));
  473. }
  474. struct ext4_xattr_info {
  475. int name_index;
  476. const char *name;
  477. const void *value;
  478. size_t value_len;
  479. };
  480. struct ext4_xattr_search {
  481. struct ext4_xattr_entry *first;
  482. void *base;
  483. void *end;
  484. struct ext4_xattr_entry *here;
  485. int not_found;
  486. };
  487. static int
  488. ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
  489. {
  490. struct ext4_xattr_entry *last;
  491. size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
  492. /* Compute min_offs and last. */
  493. last = s->first;
  494. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  495. if (!last->e_value_block && last->e_value_size) {
  496. size_t offs = le16_to_cpu(last->e_value_offs);
  497. if (offs < min_offs)
  498. min_offs = offs;
  499. }
  500. }
  501. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  502. if (!s->not_found) {
  503. if (!s->here->e_value_block && s->here->e_value_size) {
  504. size_t size = le32_to_cpu(s->here->e_value_size);
  505. free += EXT4_XATTR_SIZE(size);
  506. }
  507. free += EXT4_XATTR_LEN(name_len);
  508. }
  509. if (i->value) {
  510. if (free < EXT4_XATTR_SIZE(i->value_len) ||
  511. free < EXT4_XATTR_LEN(name_len) +
  512. EXT4_XATTR_SIZE(i->value_len))
  513. return -ENOSPC;
  514. }
  515. if (i->value && s->not_found) {
  516. /* Insert the new name. */
  517. size_t size = EXT4_XATTR_LEN(name_len);
  518. size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
  519. memmove((void *)s->here + size, s->here, rest);
  520. memset(s->here, 0, size);
  521. s->here->e_name_index = i->name_index;
  522. s->here->e_name_len = name_len;
  523. memcpy(s->here->e_name, i->name, name_len);
  524. } else {
  525. if (!s->here->e_value_block && s->here->e_value_size) {
  526. void *first_val = s->base + min_offs;
  527. size_t offs = le16_to_cpu(s->here->e_value_offs);
  528. void *val = s->base + offs;
  529. size_t size = EXT4_XATTR_SIZE(
  530. le32_to_cpu(s->here->e_value_size));
  531. if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
  532. /* The old and the new value have the same
  533. size. Just replace. */
  534. s->here->e_value_size =
  535. cpu_to_le32(i->value_len);
  536. memset(val + size - EXT4_XATTR_PAD, 0,
  537. EXT4_XATTR_PAD); /* Clear pad bytes. */
  538. memcpy(val, i->value, i->value_len);
  539. return 0;
  540. }
  541. /* Remove the old value. */
  542. memmove(first_val + size, first_val, val - first_val);
  543. memset(first_val, 0, size);
  544. s->here->e_value_size = 0;
  545. s->here->e_value_offs = 0;
  546. min_offs += size;
  547. /* Adjust all value offsets. */
  548. last = s->first;
  549. while (!IS_LAST_ENTRY(last)) {
  550. size_t o = le16_to_cpu(last->e_value_offs);
  551. if (!last->e_value_block &&
  552. last->e_value_size && o < offs)
  553. last->e_value_offs =
  554. cpu_to_le16(o + size);
  555. last = EXT4_XATTR_NEXT(last);
  556. }
  557. }
  558. if (!i->value) {
  559. /* Remove the old name. */
  560. size_t size = EXT4_XATTR_LEN(name_len);
  561. last = ENTRY((void *)last - size);
  562. memmove(s->here, (void *)s->here + size,
  563. (void *)last - (void *)s->here + sizeof(__u32));
  564. memset(last, 0, size);
  565. }
  566. }
  567. if (i->value) {
  568. /* Insert the new value. */
  569. s->here->e_value_size = cpu_to_le32(i->value_len);
  570. if (i->value_len) {
  571. size_t size = EXT4_XATTR_SIZE(i->value_len);
  572. void *val = s->base + min_offs - size;
  573. s->here->e_value_offs = cpu_to_le16(min_offs - size);
  574. memset(val + size - EXT4_XATTR_PAD, 0,
  575. EXT4_XATTR_PAD); /* Clear the pad bytes. */
  576. memcpy(val, i->value, i->value_len);
  577. }
  578. }
  579. return 0;
  580. }
  581. struct ext4_xattr_block_find {
  582. struct ext4_xattr_search s;
  583. struct buffer_head *bh;
  584. };
  585. static int
  586. ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
  587. struct ext4_xattr_block_find *bs)
  588. {
  589. struct super_block *sb = inode->i_sb;
  590. int error;
  591. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  592. i->name_index, i->name, i->value, (long)i->value_len);
  593. if (EXT4_I(inode)->i_file_acl) {
  594. /* The inode already has an extended attribute block. */
  595. bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
  596. error = -EIO;
  597. if (!bs->bh)
  598. goto cleanup;
  599. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  600. atomic_read(&(bs->bh->b_count)),
  601. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  602. if (ext4_xattr_check_block(bs->bh)) {
  603. ext4_error(sb, __FUNCTION__,
  604. "inode %lu: bad block %llu", inode->i_ino,
  605. EXT4_I(inode)->i_file_acl);
  606. error = -EIO;
  607. goto cleanup;
  608. }
  609. /* Find the named attribute. */
  610. bs->s.base = BHDR(bs->bh);
  611. bs->s.first = BFIRST(bs->bh);
  612. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  613. bs->s.here = bs->s.first;
  614. error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
  615. i->name, bs->bh->b_size, 1);
  616. if (error && error != -ENODATA)
  617. goto cleanup;
  618. bs->s.not_found = error;
  619. }
  620. error = 0;
  621. cleanup:
  622. return error;
  623. }
  624. static int
  625. ext4_xattr_block_set(handle_t *handle, struct inode *inode,
  626. struct ext4_xattr_info *i,
  627. struct ext4_xattr_block_find *bs)
  628. {
  629. struct super_block *sb = inode->i_sb;
  630. struct buffer_head *new_bh = NULL;
  631. struct ext4_xattr_search *s = &bs->s;
  632. struct mb_cache_entry *ce = NULL;
  633. int error = 0;
  634. #define header(x) ((struct ext4_xattr_header *)(x))
  635. if (i->value && i->value_len > sb->s_blocksize)
  636. return -ENOSPC;
  637. if (s->base) {
  638. ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
  639. bs->bh->b_blocknr);
  640. error = ext4_journal_get_write_access(handle, bs->bh);
  641. if (error)
  642. goto cleanup;
  643. lock_buffer(bs->bh);
  644. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  645. if (ce) {
  646. mb_cache_entry_free(ce);
  647. ce = NULL;
  648. }
  649. ea_bdebug(bs->bh, "modifying in-place");
  650. error = ext4_xattr_set_entry(i, s);
  651. if (!error) {
  652. if (!IS_LAST_ENTRY(s->first))
  653. ext4_xattr_rehash(header(s->base),
  654. s->here);
  655. ext4_xattr_cache_insert(bs->bh);
  656. }
  657. unlock_buffer(bs->bh);
  658. if (error == -EIO)
  659. goto bad_block;
  660. if (!error)
  661. error = ext4_journal_dirty_metadata(handle,
  662. bs->bh);
  663. if (error)
  664. goto cleanup;
  665. goto inserted;
  666. } else {
  667. int offset = (char *)s->here - bs->bh->b_data;
  668. unlock_buffer(bs->bh);
  669. jbd2_journal_release_buffer(handle, bs->bh);
  670. if (ce) {
  671. mb_cache_entry_release(ce);
  672. ce = NULL;
  673. }
  674. ea_bdebug(bs->bh, "cloning");
  675. s->base = kmalloc(bs->bh->b_size, GFP_KERNEL);
  676. error = -ENOMEM;
  677. if (s->base == NULL)
  678. goto cleanup;
  679. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  680. s->first = ENTRY(header(s->base)+1);
  681. header(s->base)->h_refcount = cpu_to_le32(1);
  682. s->here = ENTRY(s->base + offset);
  683. s->end = s->base + bs->bh->b_size;
  684. }
  685. } else {
  686. /* Allocate a buffer where we construct the new block. */
  687. s->base = kzalloc(sb->s_blocksize, GFP_KERNEL);
  688. /* assert(header == s->base) */
  689. error = -ENOMEM;
  690. if (s->base == NULL)
  691. goto cleanup;
  692. header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  693. header(s->base)->h_blocks = cpu_to_le32(1);
  694. header(s->base)->h_refcount = cpu_to_le32(1);
  695. s->first = ENTRY(header(s->base)+1);
  696. s->here = ENTRY(header(s->base)+1);
  697. s->end = s->base + sb->s_blocksize;
  698. }
  699. error = ext4_xattr_set_entry(i, s);
  700. if (error == -EIO)
  701. goto bad_block;
  702. if (error)
  703. goto cleanup;
  704. if (!IS_LAST_ENTRY(s->first))
  705. ext4_xattr_rehash(header(s->base), s->here);
  706. inserted:
  707. if (!IS_LAST_ENTRY(s->first)) {
  708. new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
  709. if (new_bh) {
  710. /* We found an identical block in the cache. */
  711. if (new_bh == bs->bh)
  712. ea_bdebug(new_bh, "keeping");
  713. else {
  714. /* The old block is released after updating
  715. the inode. */
  716. error = -EDQUOT;
  717. if (DQUOT_ALLOC_BLOCK(inode, 1))
  718. goto cleanup;
  719. error = ext4_journal_get_write_access(handle,
  720. new_bh);
  721. if (error)
  722. goto cleanup_dquot;
  723. lock_buffer(new_bh);
  724. le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
  725. ea_bdebug(new_bh, "reusing; refcount now=%d",
  726. le32_to_cpu(BHDR(new_bh)->h_refcount));
  727. unlock_buffer(new_bh);
  728. error = ext4_journal_dirty_metadata(handle,
  729. new_bh);
  730. if (error)
  731. goto cleanup_dquot;
  732. }
  733. mb_cache_entry_release(ce);
  734. ce = NULL;
  735. } else if (bs->bh && s->base == bs->bh->b_data) {
  736. /* We were modifying this block in-place. */
  737. ea_bdebug(bs->bh, "keeping this block");
  738. new_bh = bs->bh;
  739. get_bh(new_bh);
  740. } else {
  741. /* We need to allocate a new block */
  742. ext4_fsblk_t goal = ext4_group_first_block_no(sb,
  743. EXT4_I(inode)->i_block_group);
  744. ext4_fsblk_t block = ext4_new_block(handle, inode,
  745. goal, &error);
  746. if (error)
  747. goto cleanup;
  748. ea_idebug(inode, "creating block %d", block);
  749. new_bh = sb_getblk(sb, block);
  750. if (!new_bh) {
  751. getblk_failed:
  752. ext4_free_blocks(handle, inode, block, 1, 1);
  753. error = -EIO;
  754. goto cleanup;
  755. }
  756. lock_buffer(new_bh);
  757. error = ext4_journal_get_create_access(handle, new_bh);
  758. if (error) {
  759. unlock_buffer(new_bh);
  760. goto getblk_failed;
  761. }
  762. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  763. set_buffer_uptodate(new_bh);
  764. unlock_buffer(new_bh);
  765. ext4_xattr_cache_insert(new_bh);
  766. error = ext4_journal_dirty_metadata(handle, new_bh);
  767. if (error)
  768. goto cleanup;
  769. }
  770. }
  771. /* Update the inode. */
  772. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  773. /* Drop the previous xattr block. */
  774. if (bs->bh && bs->bh != new_bh)
  775. ext4_xattr_release_block(handle, inode, bs->bh);
  776. error = 0;
  777. cleanup:
  778. if (ce)
  779. mb_cache_entry_release(ce);
  780. brelse(new_bh);
  781. if (!(bs->bh && s->base == bs->bh->b_data))
  782. kfree(s->base);
  783. return error;
  784. cleanup_dquot:
  785. DQUOT_FREE_BLOCK(inode, 1);
  786. goto cleanup;
  787. bad_block:
  788. ext4_error(inode->i_sb, __FUNCTION__,
  789. "inode %lu: bad block %llu", inode->i_ino,
  790. EXT4_I(inode)->i_file_acl);
  791. goto cleanup;
  792. #undef header
  793. }
  794. struct ext4_xattr_ibody_find {
  795. struct ext4_xattr_search s;
  796. struct ext4_iloc iloc;
  797. };
  798. static int
  799. ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  800. struct ext4_xattr_ibody_find *is)
  801. {
  802. struct ext4_xattr_ibody_header *header;
  803. struct ext4_inode *raw_inode;
  804. int error;
  805. if (EXT4_I(inode)->i_extra_isize == 0)
  806. return 0;
  807. raw_inode = ext4_raw_inode(&is->iloc);
  808. header = IHDR(inode, raw_inode);
  809. is->s.base = is->s.first = IFIRST(header);
  810. is->s.here = is->s.first;
  811. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  812. if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
  813. error = ext4_xattr_check_names(IFIRST(header), is->s.end);
  814. if (error)
  815. return error;
  816. /* Find the named attribute. */
  817. error = ext4_xattr_find_entry(&is->s.here, i->name_index,
  818. i->name, is->s.end -
  819. (void *)is->s.base, 0);
  820. if (error && error != -ENODATA)
  821. return error;
  822. is->s.not_found = error;
  823. }
  824. return 0;
  825. }
  826. static int
  827. ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  828. struct ext4_xattr_info *i,
  829. struct ext4_xattr_ibody_find *is)
  830. {
  831. struct ext4_xattr_ibody_header *header;
  832. struct ext4_xattr_search *s = &is->s;
  833. int error;
  834. if (EXT4_I(inode)->i_extra_isize == 0)
  835. return -ENOSPC;
  836. error = ext4_xattr_set_entry(i, s);
  837. if (error)
  838. return error;
  839. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  840. if (!IS_LAST_ENTRY(s->first)) {
  841. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  842. EXT4_I(inode)->i_state |= EXT4_STATE_XATTR;
  843. } else {
  844. header->h_magic = cpu_to_le32(0);
  845. EXT4_I(inode)->i_state &= ~EXT4_STATE_XATTR;
  846. }
  847. return 0;
  848. }
  849. /*
  850. * ext4_xattr_set_handle()
  851. *
  852. * Create, replace or remove an extended attribute for this inode. Buffer
  853. * is NULL to remove an existing extended attribute, and non-NULL to
  854. * either replace an existing extended attribute, or create a new extended
  855. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  856. * specify that an extended attribute must exist and must not exist
  857. * previous to the call, respectively.
  858. *
  859. * Returns 0, or a negative error number on failure.
  860. */
  861. int
  862. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  863. const char *name, const void *value, size_t value_len,
  864. int flags)
  865. {
  866. struct ext4_xattr_info i = {
  867. .name_index = name_index,
  868. .name = name,
  869. .value = value,
  870. .value_len = value_len,
  871. };
  872. struct ext4_xattr_ibody_find is = {
  873. .s = { .not_found = -ENODATA, },
  874. };
  875. struct ext4_xattr_block_find bs = {
  876. .s = { .not_found = -ENODATA, },
  877. };
  878. int error;
  879. if (!name)
  880. return -EINVAL;
  881. if (strlen(name) > 255)
  882. return -ERANGE;
  883. down_write(&EXT4_I(inode)->xattr_sem);
  884. error = ext4_get_inode_loc(inode, &is.iloc);
  885. if (error)
  886. goto cleanup;
  887. if (EXT4_I(inode)->i_state & EXT4_STATE_NEW) {
  888. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  889. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  890. EXT4_I(inode)->i_state &= ~EXT4_STATE_NEW;
  891. }
  892. error = ext4_xattr_ibody_find(inode, &i, &is);
  893. if (error)
  894. goto cleanup;
  895. if (is.s.not_found)
  896. error = ext4_xattr_block_find(inode, &i, &bs);
  897. if (error)
  898. goto cleanup;
  899. if (is.s.not_found && bs.s.not_found) {
  900. error = -ENODATA;
  901. if (flags & XATTR_REPLACE)
  902. goto cleanup;
  903. error = 0;
  904. if (!value)
  905. goto cleanup;
  906. } else {
  907. error = -EEXIST;
  908. if (flags & XATTR_CREATE)
  909. goto cleanup;
  910. }
  911. error = ext4_journal_get_write_access(handle, is.iloc.bh);
  912. if (error)
  913. goto cleanup;
  914. if (!value) {
  915. if (!is.s.not_found)
  916. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  917. else if (!bs.s.not_found)
  918. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  919. } else {
  920. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  921. if (!error && !bs.s.not_found) {
  922. i.value = NULL;
  923. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  924. } else if (error == -ENOSPC) {
  925. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  926. if (error)
  927. goto cleanup;
  928. if (!is.s.not_found) {
  929. i.value = NULL;
  930. error = ext4_xattr_ibody_set(handle, inode, &i,
  931. &is);
  932. }
  933. }
  934. }
  935. if (!error) {
  936. ext4_xattr_update_super_block(handle, inode->i_sb);
  937. inode->i_ctime = ext4_current_time(inode);
  938. if (!value)
  939. EXT4_I(inode)->i_state &= ~EXT4_STATE_NO_EXPAND;
  940. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  941. /*
  942. * The bh is consumed by ext4_mark_iloc_dirty, even with
  943. * error != 0.
  944. */
  945. is.iloc.bh = NULL;
  946. if (IS_SYNC(inode))
  947. handle->h_sync = 1;
  948. }
  949. cleanup:
  950. brelse(is.iloc.bh);
  951. brelse(bs.bh);
  952. up_write(&EXT4_I(inode)->xattr_sem);
  953. return error;
  954. }
  955. /*
  956. * ext4_xattr_set()
  957. *
  958. * Like ext4_xattr_set_handle, but start from an inode. This extended
  959. * attribute modification is a filesystem transaction by itself.
  960. *
  961. * Returns 0, or a negative error number on failure.
  962. */
  963. int
  964. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  965. const void *value, size_t value_len, int flags)
  966. {
  967. handle_t *handle;
  968. int error, retries = 0;
  969. retry:
  970. handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
  971. if (IS_ERR(handle)) {
  972. error = PTR_ERR(handle);
  973. } else {
  974. int error2;
  975. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  976. value, value_len, flags);
  977. error2 = ext4_journal_stop(handle);
  978. if (error == -ENOSPC &&
  979. ext4_should_retry_alloc(inode->i_sb, &retries))
  980. goto retry;
  981. if (error == 0)
  982. error = error2;
  983. }
  984. return error;
  985. }
  986. /*
  987. * Shift the EA entries in the inode to create space for the increased
  988. * i_extra_isize.
  989. */
  990. static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
  991. int value_offs_shift, void *to,
  992. void *from, size_t n, int blocksize)
  993. {
  994. struct ext4_xattr_entry *last = entry;
  995. int new_offs;
  996. /* Adjust the value offsets of the entries */
  997. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  998. if (!last->e_value_block && last->e_value_size) {
  999. new_offs = le16_to_cpu(last->e_value_offs) +
  1000. value_offs_shift;
  1001. BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
  1002. > blocksize);
  1003. last->e_value_offs = cpu_to_le16(new_offs);
  1004. }
  1005. }
  1006. /* Shift the entries by n bytes */
  1007. memmove(to, from, n);
  1008. }
  1009. /*
  1010. * Expand an inode by new_extra_isize bytes when EAs are present.
  1011. * Returns 0 on success or negative error number on failure.
  1012. */
  1013. int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
  1014. struct ext4_inode *raw_inode, handle_t *handle)
  1015. {
  1016. struct ext4_xattr_ibody_header *header;
  1017. struct ext4_xattr_entry *entry, *last, *first;
  1018. struct buffer_head *bh = NULL;
  1019. struct ext4_xattr_ibody_find *is = NULL;
  1020. struct ext4_xattr_block_find *bs = NULL;
  1021. char *buffer = NULL, *b_entry_name = NULL;
  1022. size_t min_offs, free;
  1023. int total_ino, total_blk;
  1024. void *base, *start, *end;
  1025. int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
  1026. int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
  1027. down_write(&EXT4_I(inode)->xattr_sem);
  1028. retry:
  1029. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
  1030. up_write(&EXT4_I(inode)->xattr_sem);
  1031. return 0;
  1032. }
  1033. header = IHDR(inode, raw_inode);
  1034. entry = IFIRST(header);
  1035. /*
  1036. * Check if enough free space is available in the inode to shift the
  1037. * entries ahead by new_extra_isize.
  1038. */
  1039. base = start = entry;
  1040. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1041. min_offs = end - base;
  1042. last = entry;
  1043. total_ino = sizeof(struct ext4_xattr_ibody_header);
  1044. free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
  1045. if (free >= new_extra_isize) {
  1046. entry = IFIRST(header);
  1047. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
  1048. - new_extra_isize, (void *)raw_inode +
  1049. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  1050. (void *)header, total_ino,
  1051. inode->i_sb->s_blocksize);
  1052. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  1053. error = 0;
  1054. goto cleanup;
  1055. }
  1056. /*
  1057. * Enough free space isn't available in the inode, check if
  1058. * EA block can hold new_extra_isize bytes.
  1059. */
  1060. if (EXT4_I(inode)->i_file_acl) {
  1061. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1062. error = -EIO;
  1063. if (!bh)
  1064. goto cleanup;
  1065. if (ext4_xattr_check_block(bh)) {
  1066. ext4_error(inode->i_sb, __FUNCTION__,
  1067. "inode %lu: bad block %llu", inode->i_ino,
  1068. EXT4_I(inode)->i_file_acl);
  1069. error = -EIO;
  1070. goto cleanup;
  1071. }
  1072. base = BHDR(bh);
  1073. first = BFIRST(bh);
  1074. end = bh->b_data + bh->b_size;
  1075. min_offs = end - base;
  1076. free = ext4_xattr_free_space(first, &min_offs, base,
  1077. &total_blk);
  1078. if (free < new_extra_isize) {
  1079. if (!tried_min_extra_isize && s_min_extra_isize) {
  1080. tried_min_extra_isize++;
  1081. new_extra_isize = s_min_extra_isize;
  1082. brelse(bh);
  1083. goto retry;
  1084. }
  1085. error = -1;
  1086. goto cleanup;
  1087. }
  1088. } else {
  1089. free = inode->i_sb->s_blocksize;
  1090. }
  1091. while (new_extra_isize > 0) {
  1092. size_t offs, size, entry_size;
  1093. struct ext4_xattr_entry *small_entry = NULL;
  1094. struct ext4_xattr_info i = {
  1095. .value = NULL,
  1096. .value_len = 0,
  1097. };
  1098. unsigned int total_size; /* EA entry size + value size */
  1099. unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
  1100. unsigned int min_total_size = ~0U;
  1101. is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
  1102. bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
  1103. if (!is || !bs) {
  1104. error = -ENOMEM;
  1105. goto cleanup;
  1106. }
  1107. is->s.not_found = -ENODATA;
  1108. bs->s.not_found = -ENODATA;
  1109. is->iloc.bh = NULL;
  1110. bs->bh = NULL;
  1111. last = IFIRST(header);
  1112. /* Find the entry best suited to be pushed into EA block */
  1113. entry = NULL;
  1114. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1115. total_size =
  1116. EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
  1117. EXT4_XATTR_LEN(last->e_name_len);
  1118. if (total_size <= free && total_size < min_total_size) {
  1119. if (total_size < new_extra_isize) {
  1120. small_entry = last;
  1121. } else {
  1122. entry = last;
  1123. min_total_size = total_size;
  1124. }
  1125. }
  1126. }
  1127. if (entry == NULL) {
  1128. if (small_entry) {
  1129. entry = small_entry;
  1130. } else {
  1131. if (!tried_min_extra_isize &&
  1132. s_min_extra_isize) {
  1133. tried_min_extra_isize++;
  1134. new_extra_isize = s_min_extra_isize;
  1135. goto retry;
  1136. }
  1137. error = -1;
  1138. goto cleanup;
  1139. }
  1140. }
  1141. offs = le16_to_cpu(entry->e_value_offs);
  1142. size = le32_to_cpu(entry->e_value_size);
  1143. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  1144. i.name_index = entry->e_name_index,
  1145. buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
  1146. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
  1147. if (!buffer || !b_entry_name) {
  1148. error = -ENOMEM;
  1149. goto cleanup;
  1150. }
  1151. /* Save the entry name and the entry value */
  1152. memcpy(buffer, (void *)IFIRST(header) + offs,
  1153. EXT4_XATTR_SIZE(size));
  1154. memcpy(b_entry_name, entry->e_name, entry->e_name_len);
  1155. b_entry_name[entry->e_name_len] = '\0';
  1156. i.name = b_entry_name;
  1157. error = ext4_get_inode_loc(inode, &is->iloc);
  1158. if (error)
  1159. goto cleanup;
  1160. error = ext4_xattr_ibody_find(inode, &i, is);
  1161. if (error)
  1162. goto cleanup;
  1163. /* Remove the chosen entry from the inode */
  1164. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  1165. entry = IFIRST(header);
  1166. if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
  1167. shift_bytes = new_extra_isize;
  1168. else
  1169. shift_bytes = entry_size + size;
  1170. /* Adjust the offsets and shift the remaining entries ahead */
  1171. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
  1172. shift_bytes, (void *)raw_inode +
  1173. EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
  1174. (void *)header, total_ino - entry_size,
  1175. inode->i_sb->s_blocksize);
  1176. extra_isize += shift_bytes;
  1177. new_extra_isize -= shift_bytes;
  1178. EXT4_I(inode)->i_extra_isize = extra_isize;
  1179. i.name = b_entry_name;
  1180. i.value = buffer;
  1181. i.value_len = size;
  1182. error = ext4_xattr_block_find(inode, &i, bs);
  1183. if (error)
  1184. goto cleanup;
  1185. /* Add entry which was removed from the inode into the block */
  1186. error = ext4_xattr_block_set(handle, inode, &i, bs);
  1187. if (error)
  1188. goto cleanup;
  1189. kfree(b_entry_name);
  1190. kfree(buffer);
  1191. brelse(is->iloc.bh);
  1192. kfree(is);
  1193. kfree(bs);
  1194. }
  1195. brelse(bh);
  1196. up_write(&EXT4_I(inode)->xattr_sem);
  1197. return 0;
  1198. cleanup:
  1199. kfree(b_entry_name);
  1200. kfree(buffer);
  1201. if (is)
  1202. brelse(is->iloc.bh);
  1203. kfree(is);
  1204. kfree(bs);
  1205. brelse(bh);
  1206. up_write(&EXT4_I(inode)->xattr_sem);
  1207. return error;
  1208. }
  1209. /*
  1210. * ext4_xattr_delete_inode()
  1211. *
  1212. * Free extended attribute resources associated with this inode. This
  1213. * is called immediately before an inode is freed. We have exclusive
  1214. * access to the inode.
  1215. */
  1216. void
  1217. ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
  1218. {
  1219. struct buffer_head *bh = NULL;
  1220. if (!EXT4_I(inode)->i_file_acl)
  1221. goto cleanup;
  1222. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1223. if (!bh) {
  1224. ext4_error(inode->i_sb, __FUNCTION__,
  1225. "inode %lu: block %llu read error", inode->i_ino,
  1226. EXT4_I(inode)->i_file_acl);
  1227. goto cleanup;
  1228. }
  1229. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  1230. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1231. ext4_error(inode->i_sb, __FUNCTION__,
  1232. "inode %lu: bad block %llu", inode->i_ino,
  1233. EXT4_I(inode)->i_file_acl);
  1234. goto cleanup;
  1235. }
  1236. ext4_xattr_release_block(handle, inode, bh);
  1237. EXT4_I(inode)->i_file_acl = 0;
  1238. cleanup:
  1239. brelse(bh);
  1240. }
  1241. /*
  1242. * ext4_xattr_put_super()
  1243. *
  1244. * This is called when a file system is unmounted.
  1245. */
  1246. void
  1247. ext4_xattr_put_super(struct super_block *sb)
  1248. {
  1249. mb_cache_shrink(sb->s_bdev);
  1250. }
  1251. /*
  1252. * ext4_xattr_cache_insert()
  1253. *
  1254. * Create a new entry in the extended attribute cache, and insert
  1255. * it unless such an entry is already in the cache.
  1256. *
  1257. * Returns 0, or a negative error number on failure.
  1258. */
  1259. static void
  1260. ext4_xattr_cache_insert(struct buffer_head *bh)
  1261. {
  1262. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1263. struct mb_cache_entry *ce;
  1264. int error;
  1265. ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
  1266. if (!ce) {
  1267. ea_bdebug(bh, "out of memory");
  1268. return;
  1269. }
  1270. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
  1271. if (error) {
  1272. mb_cache_entry_free(ce);
  1273. if (error == -EBUSY) {
  1274. ea_bdebug(bh, "already in cache");
  1275. error = 0;
  1276. }
  1277. } else {
  1278. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1279. mb_cache_entry_release(ce);
  1280. }
  1281. }
  1282. /*
  1283. * ext4_xattr_cmp()
  1284. *
  1285. * Compare two extended attribute blocks for equality.
  1286. *
  1287. * Returns 0 if the blocks are equal, 1 if they differ, and
  1288. * a negative error number on errors.
  1289. */
  1290. static int
  1291. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  1292. struct ext4_xattr_header *header2)
  1293. {
  1294. struct ext4_xattr_entry *entry1, *entry2;
  1295. entry1 = ENTRY(header1+1);
  1296. entry2 = ENTRY(header2+1);
  1297. while (!IS_LAST_ENTRY(entry1)) {
  1298. if (IS_LAST_ENTRY(entry2))
  1299. return 1;
  1300. if (entry1->e_hash != entry2->e_hash ||
  1301. entry1->e_name_index != entry2->e_name_index ||
  1302. entry1->e_name_len != entry2->e_name_len ||
  1303. entry1->e_value_size != entry2->e_value_size ||
  1304. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1305. return 1;
  1306. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1307. return -EIO;
  1308. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1309. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1310. le32_to_cpu(entry1->e_value_size)))
  1311. return 1;
  1312. entry1 = EXT4_XATTR_NEXT(entry1);
  1313. entry2 = EXT4_XATTR_NEXT(entry2);
  1314. }
  1315. if (!IS_LAST_ENTRY(entry2))
  1316. return 1;
  1317. return 0;
  1318. }
  1319. /*
  1320. * ext4_xattr_cache_find()
  1321. *
  1322. * Find an identical extended attribute block.
  1323. *
  1324. * Returns a pointer to the block found, or NULL if such a block was
  1325. * not found or an error occurred.
  1326. */
  1327. static struct buffer_head *
  1328. ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
  1329. struct mb_cache_entry **pce)
  1330. {
  1331. __u32 hash = le32_to_cpu(header->h_hash);
  1332. struct mb_cache_entry *ce;
  1333. if (!header->h_hash)
  1334. return NULL; /* never share */
  1335. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1336. again:
  1337. ce = mb_cache_entry_find_first(ext4_xattr_cache, 0,
  1338. inode->i_sb->s_bdev, hash);
  1339. while (ce) {
  1340. struct buffer_head *bh;
  1341. if (IS_ERR(ce)) {
  1342. if (PTR_ERR(ce) == -EAGAIN)
  1343. goto again;
  1344. break;
  1345. }
  1346. bh = sb_bread(inode->i_sb, ce->e_block);
  1347. if (!bh) {
  1348. ext4_error(inode->i_sb, __FUNCTION__,
  1349. "inode %lu: block %lu read error",
  1350. inode->i_ino, (unsigned long) ce->e_block);
  1351. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1352. EXT4_XATTR_REFCOUNT_MAX) {
  1353. ea_idebug(inode, "block %lu refcount %d>=%d",
  1354. (unsigned long) ce->e_block,
  1355. le32_to_cpu(BHDR(bh)->h_refcount),
  1356. EXT4_XATTR_REFCOUNT_MAX);
  1357. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  1358. *pce = ce;
  1359. return bh;
  1360. }
  1361. brelse(bh);
  1362. ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
  1363. }
  1364. return NULL;
  1365. }
  1366. #define NAME_HASH_SHIFT 5
  1367. #define VALUE_HASH_SHIFT 16
  1368. /*
  1369. * ext4_xattr_hash_entry()
  1370. *
  1371. * Compute the hash of an extended attribute.
  1372. */
  1373. static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
  1374. struct ext4_xattr_entry *entry)
  1375. {
  1376. __u32 hash = 0;
  1377. char *name = entry->e_name;
  1378. int n;
  1379. for (n=0; n < entry->e_name_len; n++) {
  1380. hash = (hash << NAME_HASH_SHIFT) ^
  1381. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1382. *name++;
  1383. }
  1384. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1385. __le32 *value = (__le32 *)((char *)header +
  1386. le16_to_cpu(entry->e_value_offs));
  1387. for (n = (le32_to_cpu(entry->e_value_size) +
  1388. EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
  1389. hash = (hash << VALUE_HASH_SHIFT) ^
  1390. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1391. le32_to_cpu(*value++);
  1392. }
  1393. }
  1394. entry->e_hash = cpu_to_le32(hash);
  1395. }
  1396. #undef NAME_HASH_SHIFT
  1397. #undef VALUE_HASH_SHIFT
  1398. #define BLOCK_HASH_SHIFT 16
  1399. /*
  1400. * ext4_xattr_rehash()
  1401. *
  1402. * Re-compute the extended attribute hash value after an entry has changed.
  1403. */
  1404. static void ext4_xattr_rehash(struct ext4_xattr_header *header,
  1405. struct ext4_xattr_entry *entry)
  1406. {
  1407. struct ext4_xattr_entry *here;
  1408. __u32 hash = 0;
  1409. ext4_xattr_hash_entry(header, entry);
  1410. here = ENTRY(header+1);
  1411. while (!IS_LAST_ENTRY(here)) {
  1412. if (!here->e_hash) {
  1413. /* Block is not shared if an entry's hash value == 0 */
  1414. hash = 0;
  1415. break;
  1416. }
  1417. hash = (hash << BLOCK_HASH_SHIFT) ^
  1418. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1419. le32_to_cpu(here->e_hash);
  1420. here = EXT4_XATTR_NEXT(here);
  1421. }
  1422. header->h_hash = cpu_to_le32(hash);
  1423. }
  1424. #undef BLOCK_HASH_SHIFT
  1425. int __init
  1426. init_ext4_xattr(void)
  1427. {
  1428. ext4_xattr_cache = mb_cache_create("ext4_xattr", NULL,
  1429. sizeof(struct mb_cache_entry) +
  1430. sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
  1431. if (!ext4_xattr_cache)
  1432. return -ENOMEM;
  1433. return 0;
  1434. }
  1435. void
  1436. exit_ext4_xattr(void)
  1437. {
  1438. if (ext4_xattr_cache)
  1439. mb_cache_destroy(ext4_xattr_cache);
  1440. ext4_xattr_cache = NULL;
  1441. }