xattr.c 42 KB

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