xattr.c 42 KB

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