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(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  82. # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  83. #endif
  84. static void ext4_xattr_cache_insert(struct buffer_head *);
  85. static struct buffer_head *ext4_xattr_cache_find(struct inode *,
  86. struct ext4_xattr_header *,
  87. struct mb_cache_entry **);
  88. static void ext4_xattr_rehash(struct ext4_xattr_header *,
  89. struct ext4_xattr_entry *);
  90. static int ext4_xattr_list(struct dentry *dentry, char *buffer,
  91. size_t buffer_size);
  92. static struct mb_cache *ext4_xattr_cache;
  93. static const struct xattr_handler *ext4_xattr_handler_map[] = {
  94. [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
  95. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  96. [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext4_xattr_acl_access_handler,
  97. [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler,
  98. #endif
  99. [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
  100. #ifdef CONFIG_EXT4_FS_SECURITY
  101. [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
  102. #endif
  103. };
  104. const struct xattr_handler *ext4_xattr_handlers[] = {
  105. &ext4_xattr_user_handler,
  106. &ext4_xattr_trusted_handler,
  107. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  108. &ext4_xattr_acl_access_handler,
  109. &ext4_xattr_acl_default_handler,
  110. #endif
  111. #ifdef CONFIG_EXT4_FS_SECURITY
  112. &ext4_xattr_security_handler,
  113. #endif
  114. NULL
  115. };
  116. static inline const struct xattr_handler *
  117. ext4_xattr_handler(int name_index)
  118. {
  119. const struct xattr_handler *handler = NULL;
  120. if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
  121. handler = ext4_xattr_handler_map[name_index];
  122. return handler;
  123. }
  124. /*
  125. * Inode operation listxattr()
  126. *
  127. * dentry->d_inode->i_mutex: don't care
  128. */
  129. ssize_t
  130. ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
  131. {
  132. return ext4_xattr_list(dentry, buffer, size);
  133. }
  134. static int
  135. ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
  136. {
  137. while (!IS_LAST_ENTRY(entry)) {
  138. struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
  139. if ((void *)next >= end)
  140. return -EIO;
  141. entry = next;
  142. }
  143. return 0;
  144. }
  145. static inline int
  146. ext4_xattr_check_block(struct buffer_head *bh)
  147. {
  148. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  149. BHDR(bh)->h_blocks != cpu_to_le32(1))
  150. return -EIO;
  151. return ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
  152. }
  153. static inline int
  154. ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
  155. {
  156. size_t value_size = le32_to_cpu(entry->e_value_size);
  157. if (entry->e_value_block != 0 || value_size > size ||
  158. le16_to_cpu(entry->e_value_offs) + value_size > size)
  159. return -EIO;
  160. return 0;
  161. }
  162. static int
  163. ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
  164. const char *name, size_t size, int sorted)
  165. {
  166. struct ext4_xattr_entry *entry;
  167. size_t name_len;
  168. int cmp = 1;
  169. if (name == NULL)
  170. return -EINVAL;
  171. name_len = strlen(name);
  172. entry = *pentry;
  173. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  174. cmp = name_index - entry->e_name_index;
  175. if (!cmp)
  176. cmp = name_len - entry->e_name_len;
  177. if (!cmp)
  178. cmp = memcmp(name, entry->e_name, name_len);
  179. if (cmp <= 0 && (sorted || cmp == 0))
  180. break;
  181. }
  182. *pentry = entry;
  183. if (!cmp && ext4_xattr_check_entry(entry, size))
  184. return -EIO;
  185. return cmp ? -ENODATA : 0;
  186. }
  187. static int
  188. ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
  189. void *buffer, size_t buffer_size)
  190. {
  191. struct buffer_head *bh = NULL;
  192. struct ext4_xattr_entry *entry;
  193. size_t size;
  194. int error;
  195. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  196. name_index, name, buffer, (long)buffer_size);
  197. error = -ENODATA;
  198. if (!EXT4_I(inode)->i_file_acl)
  199. goto cleanup;
  200. ea_idebug(inode, "reading block %llu",
  201. (unsigned long long)EXT4_I(inode)->i_file_acl);
  202. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  203. if (!bh)
  204. goto cleanup;
  205. ea_bdebug(bh, "b_count=%d, refcount=%d",
  206. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  207. if (ext4_xattr_check_block(bh)) {
  208. bad_block:
  209. EXT4_ERROR_INODE(inode, "bad block %llu",
  210. EXT4_I(inode)->i_file_acl);
  211. error = -EIO;
  212. goto cleanup;
  213. }
  214. ext4_xattr_cache_insert(bh);
  215. entry = BFIRST(bh);
  216. error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
  217. if (error == -EIO)
  218. goto bad_block;
  219. if (error)
  220. goto cleanup;
  221. size = le32_to_cpu(entry->e_value_size);
  222. if (buffer) {
  223. error = -ERANGE;
  224. if (size > buffer_size)
  225. goto cleanup;
  226. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  227. size);
  228. }
  229. error = size;
  230. cleanup:
  231. brelse(bh);
  232. return error;
  233. }
  234. static int
  235. ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  236. void *buffer, size_t buffer_size)
  237. {
  238. struct ext4_xattr_ibody_header *header;
  239. struct ext4_xattr_entry *entry;
  240. struct ext4_inode *raw_inode;
  241. struct ext4_iloc iloc;
  242. size_t size;
  243. void *end;
  244. int error;
  245. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  246. return -ENODATA;
  247. error = ext4_get_inode_loc(inode, &iloc);
  248. if (error)
  249. return error;
  250. raw_inode = ext4_raw_inode(&iloc);
  251. header = IHDR(inode, raw_inode);
  252. entry = IFIRST(header);
  253. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  254. error = ext4_xattr_check_names(entry, end);
  255. if (error)
  256. goto cleanup;
  257. error = ext4_xattr_find_entry(&entry, name_index, name,
  258. end - (void *)entry, 0);
  259. if (error)
  260. goto cleanup;
  261. size = le32_to_cpu(entry->e_value_size);
  262. if (buffer) {
  263. error = -ERANGE;
  264. if (size > buffer_size)
  265. goto cleanup;
  266. memcpy(buffer, (void *)IFIRST(header) +
  267. le16_to_cpu(entry->e_value_offs), size);
  268. }
  269. error = size;
  270. cleanup:
  271. brelse(iloc.bh);
  272. return error;
  273. }
  274. /*
  275. * ext4_xattr_get()
  276. *
  277. * Copy an extended attribute into the buffer
  278. * provided, or compute the buffer size required.
  279. * Buffer is NULL to compute the size of the buffer required.
  280. *
  281. * Returns a negative error number on failure, or the number of bytes
  282. * used / required on success.
  283. */
  284. int
  285. ext4_xattr_get(struct inode *inode, int name_index, const char *name,
  286. void *buffer, size_t buffer_size)
  287. {
  288. int error;
  289. down_read(&EXT4_I(inode)->xattr_sem);
  290. error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
  291. buffer_size);
  292. if (error == -ENODATA)
  293. error = ext4_xattr_block_get(inode, name_index, name, buffer,
  294. buffer_size);
  295. up_read(&EXT4_I(inode)->xattr_sem);
  296. return error;
  297. }
  298. static int
  299. ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
  300. char *buffer, size_t buffer_size)
  301. {
  302. size_t rest = buffer_size;
  303. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  304. const struct xattr_handler *handler =
  305. ext4_xattr_handler(entry->e_name_index);
  306. if (handler) {
  307. size_t size = handler->list(dentry, buffer, rest,
  308. entry->e_name,
  309. entry->e_name_len,
  310. handler->flags);
  311. if (buffer) {
  312. if (size > rest)
  313. return -ERANGE;
  314. buffer += size;
  315. }
  316. rest -= size;
  317. }
  318. }
  319. return buffer_size - rest;
  320. }
  321. static int
  322. ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  323. {
  324. struct inode *inode = dentry->d_inode;
  325. struct buffer_head *bh = NULL;
  326. int error;
  327. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  328. buffer, (long)buffer_size);
  329. error = 0;
  330. if (!EXT4_I(inode)->i_file_acl)
  331. goto cleanup;
  332. ea_idebug(inode, "reading block %llu",
  333. (unsigned long long)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 ret, ret2;
  392. down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
  393. ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
  394. if (ret < 0)
  395. goto errout;
  396. if (buffer) {
  397. buffer += ret;
  398. buffer_size -= ret;
  399. }
  400. ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
  401. if (ret < 0)
  402. goto errout;
  403. ret += ret2;
  404. errout:
  405. up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
  406. return ret;
  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. ext4_handle_dirty_super(handle, sb);
  420. }
  421. }
  422. /*
  423. * Release the xattr block BH: If the reference count is > 1, decrement
  424. * it; otherwise free the block.
  425. */
  426. static void
  427. ext4_xattr_release_block(handle_t *handle, struct inode *inode,
  428. struct buffer_head *bh)
  429. {
  430. struct mb_cache_entry *ce = NULL;
  431. int error = 0;
  432. ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
  433. error = ext4_journal_get_write_access(handle, bh);
  434. if (error)
  435. goto out;
  436. lock_buffer(bh);
  437. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  438. ea_bdebug(bh, "refcount now=0; freeing");
  439. if (ce)
  440. mb_cache_entry_free(ce);
  441. get_bh(bh);
  442. ext4_free_blocks(handle, inode, bh, 0, 1,
  443. EXT4_FREE_BLOCKS_METADATA |
  444. EXT4_FREE_BLOCKS_FORGET);
  445. unlock_buffer(bh);
  446. } else {
  447. le32_add_cpu(&BHDR(bh)->h_refcount, -1);
  448. if (ce)
  449. mb_cache_entry_release(ce);
  450. unlock_buffer(bh);
  451. error = ext4_handle_dirty_metadata(handle, inode, bh);
  452. if (IS_SYNC(inode))
  453. ext4_handle_sync(handle);
  454. dquot_free_block(inode, 1);
  455. ea_bdebug(bh, "refcount now=%d; releasing",
  456. le32_to_cpu(BHDR(bh)->h_refcount));
  457. }
  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. ext4_handle_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. /*
  755. * take i_data_sem because we will test
  756. * i_delalloc_reserved_flag in ext4_mb_new_blocks
  757. */
  758. down_read((&EXT4_I(inode)->i_data_sem));
  759. block = ext4_new_meta_blocks(handle, inode, goal, 0,
  760. NULL, &error);
  761. up_read((&EXT4_I(inode)->i_data_sem));
  762. if (error)
  763. goto cleanup;
  764. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  765. BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
  766. ea_idebug(inode, "creating block %llu",
  767. (unsigned long long)block);
  768. new_bh = sb_getblk(sb, block);
  769. if (!new_bh) {
  770. getblk_failed:
  771. ext4_free_blocks(handle, inode, NULL, block, 1,
  772. EXT4_FREE_BLOCKS_METADATA);
  773. error = -EIO;
  774. goto cleanup;
  775. }
  776. lock_buffer(new_bh);
  777. error = ext4_journal_get_create_access(handle, new_bh);
  778. if (error) {
  779. unlock_buffer(new_bh);
  780. goto getblk_failed;
  781. }
  782. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  783. set_buffer_uptodate(new_bh);
  784. unlock_buffer(new_bh);
  785. ext4_xattr_cache_insert(new_bh);
  786. error = ext4_handle_dirty_metadata(handle,
  787. inode, new_bh);
  788. if (error)
  789. goto cleanup;
  790. }
  791. }
  792. /* Update the inode. */
  793. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  794. /* Drop the previous xattr block. */
  795. if (bs->bh && bs->bh != new_bh)
  796. ext4_xattr_release_block(handle, inode, bs->bh);
  797. error = 0;
  798. cleanup:
  799. if (ce)
  800. mb_cache_entry_release(ce);
  801. brelse(new_bh);
  802. if (!(bs->bh && s->base == bs->bh->b_data))
  803. kfree(s->base);
  804. return error;
  805. cleanup_dquot:
  806. dquot_free_block(inode, 1);
  807. goto cleanup;
  808. bad_block:
  809. EXT4_ERROR_INODE(inode, "bad block %llu",
  810. EXT4_I(inode)->i_file_acl);
  811. goto cleanup;
  812. #undef header
  813. }
  814. struct ext4_xattr_ibody_find {
  815. struct ext4_xattr_search s;
  816. struct ext4_iloc iloc;
  817. };
  818. static int
  819. ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  820. struct ext4_xattr_ibody_find *is)
  821. {
  822. struct ext4_xattr_ibody_header *header;
  823. struct ext4_inode *raw_inode;
  824. int error;
  825. if (EXT4_I(inode)->i_extra_isize == 0)
  826. return 0;
  827. raw_inode = ext4_raw_inode(&is->iloc);
  828. header = IHDR(inode, raw_inode);
  829. is->s.base = is->s.first = IFIRST(header);
  830. is->s.here = is->s.first;
  831. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  832. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  833. error = ext4_xattr_check_names(IFIRST(header), is->s.end);
  834. if (error)
  835. return error;
  836. /* Find the named attribute. */
  837. error = ext4_xattr_find_entry(&is->s.here, i->name_index,
  838. i->name, is->s.end -
  839. (void *)is->s.base, 0);
  840. if (error && error != -ENODATA)
  841. return error;
  842. is->s.not_found = error;
  843. }
  844. return 0;
  845. }
  846. static int
  847. ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  848. struct ext4_xattr_info *i,
  849. struct ext4_xattr_ibody_find *is)
  850. {
  851. struct ext4_xattr_ibody_header *header;
  852. struct ext4_xattr_search *s = &is->s;
  853. int error;
  854. if (EXT4_I(inode)->i_extra_isize == 0)
  855. return -ENOSPC;
  856. error = ext4_xattr_set_entry(i, s);
  857. if (error)
  858. return error;
  859. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  860. if (!IS_LAST_ENTRY(s->first)) {
  861. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  862. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  863. } else {
  864. header->h_magic = cpu_to_le32(0);
  865. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  866. }
  867. return 0;
  868. }
  869. /*
  870. * ext4_xattr_set_handle()
  871. *
  872. * Create, replace or remove an extended attribute for this inode. Value
  873. * is NULL to remove an existing extended attribute, and non-NULL to
  874. * either replace an existing extended attribute, or create a new extended
  875. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  876. * specify that an extended attribute must exist and must not exist
  877. * previous to the call, respectively.
  878. *
  879. * Returns 0, or a negative error number on failure.
  880. */
  881. int
  882. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  883. const char *name, const void *value, size_t value_len,
  884. int flags)
  885. {
  886. struct ext4_xattr_info i = {
  887. .name_index = name_index,
  888. .name = name,
  889. .value = value,
  890. .value_len = value_len,
  891. };
  892. struct ext4_xattr_ibody_find is = {
  893. .s = { .not_found = -ENODATA, },
  894. };
  895. struct ext4_xattr_block_find bs = {
  896. .s = { .not_found = -ENODATA, },
  897. };
  898. unsigned long no_expand;
  899. int error;
  900. if (!name)
  901. return -EINVAL;
  902. if (strlen(name) > 255)
  903. return -ERANGE;
  904. down_write(&EXT4_I(inode)->xattr_sem);
  905. no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
  906. ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
  907. error = ext4_reserve_inode_write(handle, inode, &is.iloc);
  908. if (error)
  909. goto cleanup;
  910. if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
  911. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  912. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  913. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  914. }
  915. error = ext4_xattr_ibody_find(inode, &i, &is);
  916. if (error)
  917. goto cleanup;
  918. if (is.s.not_found)
  919. error = ext4_xattr_block_find(inode, &i, &bs);
  920. if (error)
  921. goto cleanup;
  922. if (is.s.not_found && bs.s.not_found) {
  923. error = -ENODATA;
  924. if (flags & XATTR_REPLACE)
  925. goto cleanup;
  926. error = 0;
  927. if (!value)
  928. goto cleanup;
  929. } else {
  930. error = -EEXIST;
  931. if (flags & XATTR_CREATE)
  932. goto cleanup;
  933. }
  934. if (!value) {
  935. if (!is.s.not_found)
  936. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  937. else if (!bs.s.not_found)
  938. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  939. } else {
  940. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  941. if (!error && !bs.s.not_found) {
  942. i.value = NULL;
  943. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  944. } else if (error == -ENOSPC) {
  945. if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
  946. error = ext4_xattr_block_find(inode, &i, &bs);
  947. if (error)
  948. goto cleanup;
  949. }
  950. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  951. if (error)
  952. goto cleanup;
  953. if (!is.s.not_found) {
  954. i.value = NULL;
  955. error = ext4_xattr_ibody_set(handle, inode, &i,
  956. &is);
  957. }
  958. }
  959. }
  960. if (!error) {
  961. ext4_xattr_update_super_block(handle, inode->i_sb);
  962. inode->i_ctime = ext4_current_time(inode);
  963. if (!value)
  964. ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
  965. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  966. /*
  967. * The bh is consumed by ext4_mark_iloc_dirty, even with
  968. * error != 0.
  969. */
  970. is.iloc.bh = NULL;
  971. if (IS_SYNC(inode))
  972. ext4_handle_sync(handle);
  973. }
  974. cleanup:
  975. brelse(is.iloc.bh);
  976. brelse(bs.bh);
  977. if (no_expand == 0)
  978. ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
  979. up_write(&EXT4_I(inode)->xattr_sem);
  980. return error;
  981. }
  982. /*
  983. * ext4_xattr_set()
  984. *
  985. * Like ext4_xattr_set_handle, but start from an inode. This extended
  986. * attribute modification is a filesystem transaction by itself.
  987. *
  988. * Returns 0, or a negative error number on failure.
  989. */
  990. int
  991. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  992. const void *value, size_t value_len, int flags)
  993. {
  994. handle_t *handle;
  995. int error, retries = 0;
  996. retry:
  997. handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
  998. if (IS_ERR(handle)) {
  999. error = PTR_ERR(handle);
  1000. } else {
  1001. int error2;
  1002. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  1003. value, value_len, flags);
  1004. error2 = ext4_journal_stop(handle);
  1005. if (error == -ENOSPC &&
  1006. ext4_should_retry_alloc(inode->i_sb, &retries))
  1007. goto retry;
  1008. if (error == 0)
  1009. error = error2;
  1010. }
  1011. return error;
  1012. }
  1013. /*
  1014. * Shift the EA entries in the inode to create space for the increased
  1015. * i_extra_isize.
  1016. */
  1017. static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
  1018. int value_offs_shift, void *to,
  1019. void *from, size_t n, int blocksize)
  1020. {
  1021. struct ext4_xattr_entry *last = entry;
  1022. int new_offs;
  1023. /* Adjust the value offsets of the entries */
  1024. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1025. if (!last->e_value_block && last->e_value_size) {
  1026. new_offs = le16_to_cpu(last->e_value_offs) +
  1027. value_offs_shift;
  1028. BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
  1029. > blocksize);
  1030. last->e_value_offs = cpu_to_le16(new_offs);
  1031. }
  1032. }
  1033. /* Shift the entries by n bytes */
  1034. memmove(to, from, n);
  1035. }
  1036. /*
  1037. * Expand an inode by new_extra_isize bytes when EAs are present.
  1038. * Returns 0 on success or negative error number on failure.
  1039. */
  1040. int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
  1041. struct ext4_inode *raw_inode, handle_t *handle)
  1042. {
  1043. struct ext4_xattr_ibody_header *header;
  1044. struct ext4_xattr_entry *entry, *last, *first;
  1045. struct buffer_head *bh = NULL;
  1046. struct ext4_xattr_ibody_find *is = NULL;
  1047. struct ext4_xattr_block_find *bs = NULL;
  1048. char *buffer = NULL, *b_entry_name = NULL;
  1049. size_t min_offs, free;
  1050. int total_ino, total_blk;
  1051. void *base, *start, *end;
  1052. int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
  1053. int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
  1054. down_write(&EXT4_I(inode)->xattr_sem);
  1055. retry:
  1056. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
  1057. up_write(&EXT4_I(inode)->xattr_sem);
  1058. return 0;
  1059. }
  1060. header = IHDR(inode, raw_inode);
  1061. entry = IFIRST(header);
  1062. /*
  1063. * Check if enough free space is available in the inode to shift the
  1064. * entries ahead by new_extra_isize.
  1065. */
  1066. base = start = entry;
  1067. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1068. min_offs = end - base;
  1069. last = entry;
  1070. total_ino = sizeof(struct ext4_xattr_ibody_header);
  1071. free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
  1072. if (free >= new_extra_isize) {
  1073. entry = IFIRST(header);
  1074. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
  1075. - new_extra_isize, (void *)raw_inode +
  1076. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  1077. (void *)header, total_ino,
  1078. inode->i_sb->s_blocksize);
  1079. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  1080. error = 0;
  1081. goto cleanup;
  1082. }
  1083. /*
  1084. * Enough free space isn't available in the inode, check if
  1085. * EA block can hold new_extra_isize bytes.
  1086. */
  1087. if (EXT4_I(inode)->i_file_acl) {
  1088. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1089. error = -EIO;
  1090. if (!bh)
  1091. goto cleanup;
  1092. if (ext4_xattr_check_block(bh)) {
  1093. EXT4_ERROR_INODE(inode, "bad block %llu",
  1094. EXT4_I(inode)->i_file_acl);
  1095. error = -EIO;
  1096. goto cleanup;
  1097. }
  1098. base = BHDR(bh);
  1099. first = BFIRST(bh);
  1100. end = bh->b_data + bh->b_size;
  1101. min_offs = end - base;
  1102. free = ext4_xattr_free_space(first, &min_offs, base,
  1103. &total_blk);
  1104. if (free < new_extra_isize) {
  1105. if (!tried_min_extra_isize && s_min_extra_isize) {
  1106. tried_min_extra_isize++;
  1107. new_extra_isize = s_min_extra_isize;
  1108. brelse(bh);
  1109. goto retry;
  1110. }
  1111. error = -1;
  1112. goto cleanup;
  1113. }
  1114. } else {
  1115. free = inode->i_sb->s_blocksize;
  1116. }
  1117. while (new_extra_isize > 0) {
  1118. size_t offs, size, entry_size;
  1119. struct ext4_xattr_entry *small_entry = NULL;
  1120. struct ext4_xattr_info i = {
  1121. .value = NULL,
  1122. .value_len = 0,
  1123. };
  1124. unsigned int total_size; /* EA entry size + value size */
  1125. unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
  1126. unsigned int min_total_size = ~0U;
  1127. is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
  1128. bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
  1129. if (!is || !bs) {
  1130. error = -ENOMEM;
  1131. goto cleanup;
  1132. }
  1133. is->s.not_found = -ENODATA;
  1134. bs->s.not_found = -ENODATA;
  1135. is->iloc.bh = NULL;
  1136. bs->bh = NULL;
  1137. last = IFIRST(header);
  1138. /* Find the entry best suited to be pushed into EA block */
  1139. entry = NULL;
  1140. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1141. total_size =
  1142. EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
  1143. EXT4_XATTR_LEN(last->e_name_len);
  1144. if (total_size <= free && total_size < min_total_size) {
  1145. if (total_size < new_extra_isize) {
  1146. small_entry = last;
  1147. } else {
  1148. entry = last;
  1149. min_total_size = total_size;
  1150. }
  1151. }
  1152. }
  1153. if (entry == NULL) {
  1154. if (small_entry) {
  1155. entry = small_entry;
  1156. } else {
  1157. if (!tried_min_extra_isize &&
  1158. s_min_extra_isize) {
  1159. tried_min_extra_isize++;
  1160. new_extra_isize = s_min_extra_isize;
  1161. goto retry;
  1162. }
  1163. error = -1;
  1164. goto cleanup;
  1165. }
  1166. }
  1167. offs = le16_to_cpu(entry->e_value_offs);
  1168. size = le32_to_cpu(entry->e_value_size);
  1169. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  1170. i.name_index = entry->e_name_index,
  1171. buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
  1172. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
  1173. if (!buffer || !b_entry_name) {
  1174. error = -ENOMEM;
  1175. goto cleanup;
  1176. }
  1177. /* Save the entry name and the entry value */
  1178. memcpy(buffer, (void *)IFIRST(header) + offs,
  1179. EXT4_XATTR_SIZE(size));
  1180. memcpy(b_entry_name, entry->e_name, entry->e_name_len);
  1181. b_entry_name[entry->e_name_len] = '\0';
  1182. i.name = b_entry_name;
  1183. error = ext4_get_inode_loc(inode, &is->iloc);
  1184. if (error)
  1185. goto cleanup;
  1186. error = ext4_xattr_ibody_find(inode, &i, is);
  1187. if (error)
  1188. goto cleanup;
  1189. /* Remove the chosen entry from the inode */
  1190. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  1191. if (error)
  1192. goto cleanup;
  1193. entry = IFIRST(header);
  1194. if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
  1195. shift_bytes = new_extra_isize;
  1196. else
  1197. shift_bytes = entry_size + size;
  1198. /* Adjust the offsets and shift the remaining entries ahead */
  1199. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
  1200. shift_bytes, (void *)raw_inode +
  1201. EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
  1202. (void *)header, total_ino - entry_size,
  1203. inode->i_sb->s_blocksize);
  1204. extra_isize += shift_bytes;
  1205. new_extra_isize -= shift_bytes;
  1206. EXT4_I(inode)->i_extra_isize = extra_isize;
  1207. i.name = b_entry_name;
  1208. i.value = buffer;
  1209. i.value_len = size;
  1210. error = ext4_xattr_block_find(inode, &i, bs);
  1211. if (error)
  1212. goto cleanup;
  1213. /* Add entry which was removed from the inode into the block */
  1214. error = ext4_xattr_block_set(handle, inode, &i, bs);
  1215. if (error)
  1216. goto cleanup;
  1217. kfree(b_entry_name);
  1218. kfree(buffer);
  1219. b_entry_name = NULL;
  1220. buffer = NULL;
  1221. brelse(is->iloc.bh);
  1222. kfree(is);
  1223. kfree(bs);
  1224. }
  1225. brelse(bh);
  1226. up_write(&EXT4_I(inode)->xattr_sem);
  1227. return 0;
  1228. cleanup:
  1229. kfree(b_entry_name);
  1230. kfree(buffer);
  1231. if (is)
  1232. brelse(is->iloc.bh);
  1233. kfree(is);
  1234. kfree(bs);
  1235. brelse(bh);
  1236. up_write(&EXT4_I(inode)->xattr_sem);
  1237. return error;
  1238. }
  1239. /*
  1240. * ext4_xattr_delete_inode()
  1241. *
  1242. * Free extended attribute resources associated with this inode. This
  1243. * is called immediately before an inode is freed. We have exclusive
  1244. * access to the inode.
  1245. */
  1246. void
  1247. ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
  1248. {
  1249. struct buffer_head *bh = NULL;
  1250. if (!EXT4_I(inode)->i_file_acl)
  1251. goto cleanup;
  1252. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1253. if (!bh) {
  1254. EXT4_ERROR_INODE(inode, "block %llu read error",
  1255. EXT4_I(inode)->i_file_acl);
  1256. goto cleanup;
  1257. }
  1258. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  1259. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1260. EXT4_ERROR_INODE(inode, "bad block %llu",
  1261. EXT4_I(inode)->i_file_acl);
  1262. goto cleanup;
  1263. }
  1264. ext4_xattr_release_block(handle, inode, bh);
  1265. EXT4_I(inode)->i_file_acl = 0;
  1266. cleanup:
  1267. brelse(bh);
  1268. }
  1269. /*
  1270. * ext4_xattr_put_super()
  1271. *
  1272. * This is called when a file system is unmounted.
  1273. */
  1274. void
  1275. ext4_xattr_put_super(struct super_block *sb)
  1276. {
  1277. mb_cache_shrink(sb->s_bdev);
  1278. }
  1279. /*
  1280. * ext4_xattr_cache_insert()
  1281. *
  1282. * Create a new entry in the extended attribute cache, and insert
  1283. * it unless such an entry is already in the cache.
  1284. *
  1285. * Returns 0, or a negative error number on failure.
  1286. */
  1287. static void
  1288. ext4_xattr_cache_insert(struct buffer_head *bh)
  1289. {
  1290. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1291. struct mb_cache_entry *ce;
  1292. int error;
  1293. ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
  1294. if (!ce) {
  1295. ea_bdebug(bh, "out of memory");
  1296. return;
  1297. }
  1298. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
  1299. if (error) {
  1300. mb_cache_entry_free(ce);
  1301. if (error == -EBUSY) {
  1302. ea_bdebug(bh, "already in cache");
  1303. error = 0;
  1304. }
  1305. } else {
  1306. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1307. mb_cache_entry_release(ce);
  1308. }
  1309. }
  1310. /*
  1311. * ext4_xattr_cmp()
  1312. *
  1313. * Compare two extended attribute blocks for equality.
  1314. *
  1315. * Returns 0 if the blocks are equal, 1 if they differ, and
  1316. * a negative error number on errors.
  1317. */
  1318. static int
  1319. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  1320. struct ext4_xattr_header *header2)
  1321. {
  1322. struct ext4_xattr_entry *entry1, *entry2;
  1323. entry1 = ENTRY(header1+1);
  1324. entry2 = ENTRY(header2+1);
  1325. while (!IS_LAST_ENTRY(entry1)) {
  1326. if (IS_LAST_ENTRY(entry2))
  1327. return 1;
  1328. if (entry1->e_hash != entry2->e_hash ||
  1329. entry1->e_name_index != entry2->e_name_index ||
  1330. entry1->e_name_len != entry2->e_name_len ||
  1331. entry1->e_value_size != entry2->e_value_size ||
  1332. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1333. return 1;
  1334. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1335. return -EIO;
  1336. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1337. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1338. le32_to_cpu(entry1->e_value_size)))
  1339. return 1;
  1340. entry1 = EXT4_XATTR_NEXT(entry1);
  1341. entry2 = EXT4_XATTR_NEXT(entry2);
  1342. }
  1343. if (!IS_LAST_ENTRY(entry2))
  1344. return 1;
  1345. return 0;
  1346. }
  1347. /*
  1348. * ext4_xattr_cache_find()
  1349. *
  1350. * Find an identical extended attribute block.
  1351. *
  1352. * Returns a pointer to the block found, or NULL if such a block was
  1353. * not found or an error occurred.
  1354. */
  1355. static struct buffer_head *
  1356. ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
  1357. struct mb_cache_entry **pce)
  1358. {
  1359. __u32 hash = le32_to_cpu(header->h_hash);
  1360. struct mb_cache_entry *ce;
  1361. if (!header->h_hash)
  1362. return NULL; /* never share */
  1363. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1364. again:
  1365. ce = mb_cache_entry_find_first(ext4_xattr_cache, inode->i_sb->s_bdev,
  1366. hash);
  1367. while (ce) {
  1368. struct buffer_head *bh;
  1369. if (IS_ERR(ce)) {
  1370. if (PTR_ERR(ce) == -EAGAIN)
  1371. goto again;
  1372. break;
  1373. }
  1374. bh = sb_bread(inode->i_sb, ce->e_block);
  1375. if (!bh) {
  1376. EXT4_ERROR_INODE(inode, "block %lu read error",
  1377. (unsigned long) ce->e_block);
  1378. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1379. EXT4_XATTR_REFCOUNT_MAX) {
  1380. ea_idebug(inode, "block %lu refcount %d>=%d",
  1381. (unsigned long) ce->e_block,
  1382. le32_to_cpu(BHDR(bh)->h_refcount),
  1383. EXT4_XATTR_REFCOUNT_MAX);
  1384. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  1385. *pce = ce;
  1386. return bh;
  1387. }
  1388. brelse(bh);
  1389. ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
  1390. }
  1391. return NULL;
  1392. }
  1393. #define NAME_HASH_SHIFT 5
  1394. #define VALUE_HASH_SHIFT 16
  1395. /*
  1396. * ext4_xattr_hash_entry()
  1397. *
  1398. * Compute the hash of an extended attribute.
  1399. */
  1400. static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
  1401. struct ext4_xattr_entry *entry)
  1402. {
  1403. __u32 hash = 0;
  1404. char *name = entry->e_name;
  1405. int n;
  1406. for (n = 0; n < entry->e_name_len; n++) {
  1407. hash = (hash << NAME_HASH_SHIFT) ^
  1408. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1409. *name++;
  1410. }
  1411. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1412. __le32 *value = (__le32 *)((char *)header +
  1413. le16_to_cpu(entry->e_value_offs));
  1414. for (n = (le32_to_cpu(entry->e_value_size) +
  1415. EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
  1416. hash = (hash << VALUE_HASH_SHIFT) ^
  1417. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1418. le32_to_cpu(*value++);
  1419. }
  1420. }
  1421. entry->e_hash = cpu_to_le32(hash);
  1422. }
  1423. #undef NAME_HASH_SHIFT
  1424. #undef VALUE_HASH_SHIFT
  1425. #define BLOCK_HASH_SHIFT 16
  1426. /*
  1427. * ext4_xattr_rehash()
  1428. *
  1429. * Re-compute the extended attribute hash value after an entry has changed.
  1430. */
  1431. static void ext4_xattr_rehash(struct ext4_xattr_header *header,
  1432. struct ext4_xattr_entry *entry)
  1433. {
  1434. struct ext4_xattr_entry *here;
  1435. __u32 hash = 0;
  1436. ext4_xattr_hash_entry(header, entry);
  1437. here = ENTRY(header+1);
  1438. while (!IS_LAST_ENTRY(here)) {
  1439. if (!here->e_hash) {
  1440. /* Block is not shared if an entry's hash value == 0 */
  1441. hash = 0;
  1442. break;
  1443. }
  1444. hash = (hash << BLOCK_HASH_SHIFT) ^
  1445. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1446. le32_to_cpu(here->e_hash);
  1447. here = EXT4_XATTR_NEXT(here);
  1448. }
  1449. header->h_hash = cpu_to_le32(hash);
  1450. }
  1451. #undef BLOCK_HASH_SHIFT
  1452. int __init
  1453. ext4_init_xattr(void)
  1454. {
  1455. ext4_xattr_cache = mb_cache_create("ext4_xattr", 6);
  1456. if (!ext4_xattr_cache)
  1457. return -ENOMEM;
  1458. return 0;
  1459. }
  1460. void
  1461. ext4_exit_xattr(void)
  1462. {
  1463. if (ext4_xattr_cache)
  1464. mb_cache_destroy(ext4_xattr_cache);
  1465. ext4_xattr_cache = NULL;
  1466. }