xattr.c 42 KB

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