xattr.c 42 KB

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