xattr.c 42 KB

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