xattr.c 42 KB

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