xattr.c 41 KB

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