xattr.c 45 KB

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