xattr.c 42 KB

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