xattr.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /*
  2. * linux/fs/reiserfs/xattr.c
  3. *
  4. * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
  5. *
  6. */
  7. /*
  8. * In order to implement EA/ACLs in a clean, backwards compatible manner,
  9. * they are implemented as files in a "private" directory.
  10. * Each EA is in it's own file, with the directory layout like so (/ is assumed
  11. * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
  12. * directories named using the capital-hex form of the objectid and
  13. * generation number are used. Inside each directory are individual files
  14. * named with the name of the extended attribute.
  15. *
  16. * So, for objectid 12648430, we could have:
  17. * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
  18. * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
  19. * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
  20. * .. or similar.
  21. *
  22. * The file contents are the text of the EA. The size is known based on the
  23. * stat data describing the file.
  24. *
  25. * In the case of system.posix_acl_access and system.posix_acl_default, since
  26. * these are special cases for filesystem ACLs, they are interpreted by the
  27. * kernel, in addition, they are negatively and positively cached and attached
  28. * to the inode so that unnecessary lookups are avoided.
  29. *
  30. * Locking works like so:
  31. * Directory components (xattr root, xattr dir) are protectd by their i_mutex.
  32. * The xattrs themselves are protected by the xattr_sem.
  33. */
  34. #include <linux/reiserfs_fs.h>
  35. #include <linux/capability.h>
  36. #include <linux/dcache.h>
  37. #include <linux/namei.h>
  38. #include <linux/errno.h>
  39. #include <linux/fs.h>
  40. #include <linux/file.h>
  41. #include <linux/pagemap.h>
  42. #include <linux/xattr.h>
  43. #include <linux/reiserfs_xattr.h>
  44. #include <linux/reiserfs_acl.h>
  45. #include <asm/uaccess.h>
  46. #include <net/checksum.h>
  47. #include <linux/smp_lock.h>
  48. #include <linux/stat.h>
  49. #include <linux/quotaops.h>
  50. #define PRIVROOT_NAME ".reiserfs_priv"
  51. #define XAROOT_NAME "xattrs"
  52. static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char *);
  53. /* Helpers for inode ops. We do this so that we don't have all the VFS
  54. * overhead and also for proper i_mutex annotation.
  55. * dir->i_mutex must be held for all of them. */
  56. static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
  57. {
  58. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  59. DQUOT_INIT(dir);
  60. return dir->i_op->create(dir, dentry, mode, NULL);
  61. }
  62. static int xattr_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  63. {
  64. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  65. DQUOT_INIT(dir);
  66. return dir->i_op->mkdir(dir, dentry, mode);
  67. }
  68. /* We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
  69. * mutation ops aren't called during rename or splace, which are the
  70. * only other users of I_MUTEX_CHILD. It violates the ordering, but that's
  71. * better than allocating another subclass just for this code. */
  72. static int xattr_unlink(struct inode *dir, struct dentry *dentry)
  73. {
  74. int error;
  75. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  76. DQUOT_INIT(dir);
  77. mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
  78. error = dir->i_op->unlink(dir, dentry);
  79. mutex_unlock(&dentry->d_inode->i_mutex);
  80. if (!error)
  81. d_delete(dentry);
  82. return error;
  83. }
  84. static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
  85. {
  86. int error;
  87. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  88. DQUOT_INIT(dir);
  89. mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
  90. dentry_unhash(dentry);
  91. error = dir->i_op->rmdir(dir, dentry);
  92. if (!error)
  93. dentry->d_inode->i_flags |= S_DEAD;
  94. mutex_unlock(&dentry->d_inode->i_mutex);
  95. if (!error)
  96. d_delete(dentry);
  97. dput(dentry);
  98. return error;
  99. }
  100. #define xattr_may_create(flags) (!flags || flags & XATTR_CREATE)
  101. /* Returns and possibly creates the xattr dir. */
  102. static struct dentry *lookup_or_create_dir(struct dentry *parent,
  103. const char *name, int flags)
  104. {
  105. struct dentry *dentry;
  106. BUG_ON(!parent);
  107. dentry = lookup_one_len(name, parent, strlen(name));
  108. if (IS_ERR(dentry))
  109. return dentry;
  110. else if (!dentry->d_inode) {
  111. int err = -ENODATA;
  112. if (xattr_may_create(flags)) {
  113. mutex_lock_nested(&parent->d_inode->i_mutex,
  114. I_MUTEX_XATTR);
  115. err = xattr_mkdir(parent->d_inode, dentry, 0700);
  116. mutex_unlock(&parent->d_inode->i_mutex);
  117. }
  118. if (err) {
  119. dput(dentry);
  120. dentry = ERR_PTR(err);
  121. }
  122. }
  123. return dentry;
  124. }
  125. static struct dentry *open_xa_root(struct super_block *sb, int flags)
  126. {
  127. struct dentry *privroot = REISERFS_SB(sb)->priv_root;
  128. if (!privroot)
  129. return ERR_PTR(-ENODATA);
  130. return lookup_or_create_dir(privroot, XAROOT_NAME, flags);
  131. }
  132. static struct dentry *open_xa_dir(const struct inode *inode, int flags)
  133. {
  134. struct dentry *xaroot, *xadir;
  135. char namebuf[17];
  136. xaroot = open_xa_root(inode->i_sb, flags);
  137. if (IS_ERR(xaroot))
  138. return xaroot;
  139. snprintf(namebuf, sizeof(namebuf), "%X.%X",
  140. le32_to_cpu(INODE_PKEY(inode)->k_objectid),
  141. inode->i_generation);
  142. xadir = lookup_or_create_dir(xaroot, namebuf, flags);
  143. dput(xaroot);
  144. return xadir;
  145. }
  146. /*
  147. * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
  148. * we need to drop the path before calling the filldir struct. That
  149. * would be a big performance hit to the non-xattr case, so I've copied
  150. * the whole thing for now. --clm
  151. *
  152. * the big difference is that I go backwards through the directory,
  153. * and don't mess with f->f_pos, but the idea is the same. Do some
  154. * action on each and every entry in the directory.
  155. *
  156. * we're called with i_mutex held, so there are no worries about the directory
  157. * changing underneath us.
  158. */
  159. static int __xattr_readdir(struct inode *inode, void *dirent, filldir_t filldir)
  160. {
  161. struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
  162. INITIALIZE_PATH(path_to_entry);
  163. struct buffer_head *bh;
  164. int entry_num;
  165. struct item_head *ih, tmp_ih;
  166. int search_res;
  167. char *local_buf;
  168. loff_t next_pos;
  169. char small_buf[32]; /* avoid kmalloc if we can */
  170. struct reiserfs_de_head *deh;
  171. int d_reclen;
  172. char *d_name;
  173. off_t d_off;
  174. ino_t d_ino;
  175. struct reiserfs_dir_entry de;
  176. /* form key for search the next directory entry using f_pos field of
  177. file structure */
  178. next_pos = max_reiserfs_offset(inode);
  179. while (1) {
  180. research:
  181. if (next_pos <= DOT_DOT_OFFSET)
  182. break;
  183. make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
  184. search_res =
  185. search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
  186. &de);
  187. if (search_res == IO_ERROR) {
  188. // FIXME: we could just skip part of directory which could
  189. // not be read
  190. pathrelse(&path_to_entry);
  191. return -EIO;
  192. }
  193. if (search_res == NAME_NOT_FOUND)
  194. de.de_entry_num--;
  195. set_de_name_and_namelen(&de);
  196. entry_num = de.de_entry_num;
  197. deh = &(de.de_deh[entry_num]);
  198. bh = de.de_bh;
  199. ih = de.de_ih;
  200. if (!is_direntry_le_ih(ih)) {
  201. reiserfs_error(inode->i_sb, "jdm-20000",
  202. "not direntry %h", ih);
  203. break;
  204. }
  205. copy_item_head(&tmp_ih, ih);
  206. /* we must have found item, that is item of this directory, */
  207. RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
  208. "vs-9000: found item %h does not match to dir we readdir %K",
  209. ih, &pos_key);
  210. if (deh_offset(deh) <= DOT_DOT_OFFSET) {
  211. break;
  212. }
  213. /* look for the previous entry in the directory */
  214. next_pos = deh_offset(deh) - 1;
  215. if (!de_visible(deh))
  216. /* it is hidden entry */
  217. continue;
  218. d_reclen = entry_length(bh, ih, entry_num);
  219. d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
  220. d_off = deh_offset(deh);
  221. d_ino = deh_objectid(deh);
  222. if (!d_name[d_reclen - 1])
  223. d_reclen = strlen(d_name);
  224. if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
  225. /* too big to send back to VFS */
  226. continue;
  227. }
  228. /* Ignore the .reiserfs_priv entry */
  229. if (reiserfs_xattrs(inode->i_sb) &&
  230. !old_format_only(inode->i_sb) &&
  231. deh_objectid(deh) ==
  232. le32_to_cpu(INODE_PKEY
  233. (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
  234. k_objectid))
  235. continue;
  236. if (d_reclen <= 32) {
  237. local_buf = small_buf;
  238. } else {
  239. local_buf = kmalloc(d_reclen, GFP_NOFS);
  240. if (!local_buf) {
  241. pathrelse(&path_to_entry);
  242. return -ENOMEM;
  243. }
  244. if (item_moved(&tmp_ih, &path_to_entry)) {
  245. kfree(local_buf);
  246. /* sigh, must retry. Do this same offset again */
  247. next_pos = d_off;
  248. goto research;
  249. }
  250. }
  251. // Note, that we copy name to user space via temporary
  252. // buffer (local_buf) because filldir will block if
  253. // user space buffer is swapped out. At that time
  254. // entry can move to somewhere else
  255. memcpy(local_buf, d_name, d_reclen);
  256. /* the filldir function might need to start transactions,
  257. * or do who knows what. Release the path now that we've
  258. * copied all the important stuff out of the deh
  259. */
  260. pathrelse(&path_to_entry);
  261. if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
  262. DT_UNKNOWN) < 0) {
  263. if (local_buf != small_buf) {
  264. kfree(local_buf);
  265. }
  266. goto end;
  267. }
  268. if (local_buf != small_buf) {
  269. kfree(local_buf);
  270. }
  271. } /* while */
  272. end:
  273. pathrelse(&path_to_entry);
  274. return 0;
  275. }
  276. /*
  277. * this could be done with dedicated readdir ops for the xattr files,
  278. * but I want to get something working asap
  279. * this is stolen from vfs_readdir
  280. *
  281. */
  282. static
  283. int xattr_readdir(struct inode *inode, filldir_t filler, void *buf)
  284. {
  285. int res = -ENOENT;
  286. if (!IS_DEADDIR(inode)) {
  287. lock_kernel();
  288. res = __xattr_readdir(inode, buf, filler);
  289. unlock_kernel();
  290. }
  291. return res;
  292. }
  293. /* expects xadir->d_inode->i_mutex to be locked */
  294. static int
  295. __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
  296. {
  297. struct dentry *dentry;
  298. struct inode *dir = xadir->d_inode;
  299. int err = 0;
  300. struct reiserfs_xattr_handler *xah;
  301. dentry = lookup_one_len(name, xadir, namelen);
  302. if (IS_ERR(dentry)) {
  303. err = PTR_ERR(dentry);
  304. goto out;
  305. } else if (!dentry->d_inode) {
  306. err = -ENODATA;
  307. goto out_file;
  308. }
  309. /* Skip directories.. */
  310. if (S_ISDIR(dentry->d_inode->i_mode))
  311. goto out_file;
  312. if (!IS_PRIVATE(dentry->d_inode)) {
  313. reiserfs_error(dir->i_sb, "jdm-20003",
  314. "OID %08x [%.*s/%.*s] doesn't have "
  315. "priv flag set [parent is %sset].",
  316. le32_to_cpu(INODE_PKEY(dentry->d_inode)->
  317. k_objectid), xadir->d_name.len,
  318. xadir->d_name.name, namelen, name,
  319. IS_PRIVATE(xadir->d_inode) ? "" :
  320. "not ");
  321. dput(dentry);
  322. return -EIO;
  323. }
  324. /* Deletion pre-operation */
  325. xah = find_xattr_handler_prefix(name);
  326. if (xah && xah->del) {
  327. err = xah->del(dentry->d_inode, name);
  328. if (err)
  329. goto out;
  330. }
  331. err = xattr_unlink(dir, dentry);
  332. out_file:
  333. dput(dentry);
  334. out:
  335. return err;
  336. }
  337. /* The following are side effects of other operations that aren't explicitly
  338. * modifying extended attributes. This includes operations such as permissions
  339. * or ownership changes, object deletions, etc. */
  340. static int
  341. reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
  342. loff_t offset, u64 ino, unsigned int d_type)
  343. {
  344. struct dentry *xadir = (struct dentry *)buf;
  345. return __reiserfs_xattr_del(xadir, name, namelen);
  346. }
  347. /* This is called w/ inode->i_mutex downed */
  348. int reiserfs_delete_xattrs(struct inode *inode)
  349. {
  350. int err = -ENODATA;
  351. struct dentry *dir, *root;
  352. struct reiserfs_transaction_handle th;
  353. int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
  354. 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
  355. /* Skip out, an xattr has no xattrs associated with it */
  356. if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
  357. return 0;
  358. dir = open_xa_dir(inode, XATTR_REPLACE);
  359. if (IS_ERR(dir)) {
  360. err = PTR_ERR(dir);
  361. goto out;
  362. } else if (!dir->d_inode) {
  363. dput(dir);
  364. goto out;
  365. }
  366. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
  367. err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir);
  368. mutex_unlock(&dir->d_inode->i_mutex);
  369. if (err) {
  370. dput(dir);
  371. goto out;
  372. }
  373. root = dget(dir->d_parent);
  374. dput(dir);
  375. /* We start a transaction here to avoid a ABBA situation
  376. * between the xattr root's i_mutex and the journal lock.
  377. * Inode creation will inherit an ACL, which requires a
  378. * lookup. The lookup locks the xattr root i_mutex with a
  379. * transaction open. Inode deletion takes teh xattr root
  380. * i_mutex to delete the directory and then starts a
  381. * transaction inside it. Boom. This doesn't incur much
  382. * additional overhead since the reiserfs_rmdir transaction
  383. * will just nest inside the outer transaction. */
  384. err = journal_begin(&th, inode->i_sb, blocks);
  385. if (!err) {
  386. int jerror;
  387. mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_XATTR);
  388. err = xattr_rmdir(root->d_inode, dir);
  389. jerror = journal_end(&th, inode->i_sb, blocks);
  390. mutex_unlock(&root->d_inode->i_mutex);
  391. err = jerror ?: err;
  392. }
  393. dput(root);
  394. out:
  395. if (err)
  396. reiserfs_warning(inode->i_sb, "jdm-20004",
  397. "Couldn't remove all xattrs (%d)\n", err);
  398. return err;
  399. }
  400. struct reiserfs_chown_buf {
  401. struct inode *inode;
  402. struct dentry *xadir;
  403. struct iattr *attrs;
  404. };
  405. /* XXX: If there is a better way to do this, I'd love to hear about it */
  406. static int
  407. reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
  408. loff_t offset, u64 ino, unsigned int d_type)
  409. {
  410. struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
  411. struct dentry *xafile, *xadir = chown_buf->xadir;
  412. struct iattr *attrs = chown_buf->attrs;
  413. int err = 0;
  414. xafile = lookup_one_len(name, xadir, namelen);
  415. if (IS_ERR(xafile))
  416. return PTR_ERR(xafile);
  417. else if (!xafile->d_inode) {
  418. dput(xafile);
  419. return -ENODATA;
  420. }
  421. if (!S_ISDIR(xafile->d_inode->i_mode)) {
  422. mutex_lock_nested(&xafile->d_inode->i_mutex, I_MUTEX_CHILD);
  423. err = reiserfs_setattr(xafile, attrs);
  424. mutex_unlock(&xafile->d_inode->i_mutex);
  425. }
  426. dput(xafile);
  427. return err;
  428. }
  429. int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  430. {
  431. struct dentry *dir;
  432. int err = 0;
  433. struct reiserfs_chown_buf buf;
  434. unsigned int ia_valid = attrs->ia_valid;
  435. /* Skip out, an xattr has no xattrs associated with it */
  436. if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
  437. return 0;
  438. dir = open_xa_dir(inode, XATTR_REPLACE);
  439. if (IS_ERR(dir)) {
  440. if (PTR_ERR(dir) != -ENODATA)
  441. err = PTR_ERR(dir);
  442. goto out;
  443. } else if (!dir->d_inode)
  444. goto out_dir;
  445. attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
  446. buf.xadir = dir;
  447. buf.attrs = attrs;
  448. buf.inode = inode;
  449. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
  450. err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf);
  451. if (!err)
  452. err = reiserfs_setattr(dir, attrs);
  453. mutex_unlock(&dir->d_inode->i_mutex);
  454. attrs->ia_valid = ia_valid;
  455. out_dir:
  456. dput(dir);
  457. out:
  458. if (err)
  459. reiserfs_warning(inode->i_sb, "jdm-20007",
  460. "Couldn't chown all xattrs (%d)\n", err);
  461. return err;
  462. }
  463. #ifdef CONFIG_REISERFS_FS_XATTR
  464. static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
  465. *prefix);
  466. /* Returns a dentry corresponding to a specific extended attribute file
  467. * for the inode. If flags allow, the file is created. Otherwise, a
  468. * valid or negative dentry, or an error is returned. */
  469. static struct dentry *get_xa_file_dentry(const struct inode *inode,
  470. const char *name, int flags)
  471. {
  472. struct dentry *xadir, *xafile;
  473. int err = 0;
  474. xadir = open_xa_dir(inode, flags);
  475. if (IS_ERR(xadir))
  476. return ERR_CAST(xadir);
  477. xafile = lookup_one_len(name, xadir, strlen(name));
  478. if (IS_ERR(xafile)) {
  479. err = PTR_ERR(xafile);
  480. goto out;
  481. }
  482. if (xafile->d_inode && (flags & XATTR_CREATE))
  483. err = -EEXIST;
  484. if (!xafile->d_inode) {
  485. err = -ENODATA;
  486. if (xattr_may_create(flags)) {
  487. mutex_lock_nested(&xadir->d_inode->i_mutex,
  488. I_MUTEX_XATTR);
  489. err = xattr_create(xadir->d_inode, xafile,
  490. 0700|S_IFREG);
  491. mutex_unlock(&xadir->d_inode->i_mutex);
  492. }
  493. }
  494. if (err)
  495. dput(xafile);
  496. out:
  497. dput(xadir);
  498. if (err)
  499. return ERR_PTR(err);
  500. return xafile;
  501. }
  502. /* Internal operations on file data */
  503. static inline void reiserfs_put_page(struct page *page)
  504. {
  505. kunmap(page);
  506. page_cache_release(page);
  507. }
  508. static struct page *reiserfs_get_page(struct inode *dir, size_t n)
  509. {
  510. struct address_space *mapping = dir->i_mapping;
  511. struct page *page;
  512. /* We can deadlock if we try to free dentries,
  513. and an unlink/rmdir has just occured - GFP_NOFS avoids this */
  514. mapping_set_gfp_mask(mapping, GFP_NOFS);
  515. page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL);
  516. if (!IS_ERR(page)) {
  517. kmap(page);
  518. if (PageError(page))
  519. goto fail;
  520. }
  521. return page;
  522. fail:
  523. reiserfs_put_page(page);
  524. return ERR_PTR(-EIO);
  525. }
  526. static inline __u32 xattr_hash(const char *msg, int len)
  527. {
  528. return csum_partial(msg, len, 0);
  529. }
  530. int reiserfs_commit_write(struct file *f, struct page *page,
  531. unsigned from, unsigned to);
  532. int reiserfs_prepare_write(struct file *f, struct page *page,
  533. unsigned from, unsigned to);
  534. /* Generic extended attribute operations that can be used by xa plugins */
  535. /*
  536. * inode->i_mutex: down
  537. */
  538. int
  539. reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
  540. size_t buffer_size, int flags)
  541. {
  542. int err = 0;
  543. struct dentry *dentry;
  544. struct page *page;
  545. char *data;
  546. size_t file_pos = 0;
  547. size_t buffer_pos = 0;
  548. struct iattr newattrs;
  549. __u32 xahash = 0;
  550. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  551. return -EOPNOTSUPP;
  552. if (!buffer)
  553. return reiserfs_xattr_del(inode, name);
  554. dentry = get_xa_file_dentry(inode, name, flags);
  555. if (IS_ERR(dentry)) {
  556. err = PTR_ERR(dentry);
  557. goto out;
  558. }
  559. down_write(&REISERFS_I(inode)->i_xattr_sem);
  560. xahash = xattr_hash(buffer, buffer_size);
  561. /* Resize it so we're ok to write there */
  562. newattrs.ia_size = buffer_size;
  563. newattrs.ia_ctime = current_fs_time(inode->i_sb);
  564. newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
  565. mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
  566. down_write(&dentry->d_inode->i_alloc_sem);
  567. err = reiserfs_setattr(dentry, &newattrs);
  568. up_write(&dentry->d_inode->i_alloc_sem);
  569. mutex_unlock(&dentry->d_inode->i_mutex);
  570. if (err)
  571. goto out_filp;
  572. while (buffer_pos < buffer_size || buffer_pos == 0) {
  573. size_t chunk;
  574. size_t skip = 0;
  575. size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
  576. if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
  577. chunk = PAGE_CACHE_SIZE;
  578. else
  579. chunk = buffer_size - buffer_pos;
  580. page = reiserfs_get_page(dentry->d_inode, file_pos);
  581. if (IS_ERR(page)) {
  582. err = PTR_ERR(page);
  583. goto out_filp;
  584. }
  585. lock_page(page);
  586. data = page_address(page);
  587. if (file_pos == 0) {
  588. struct reiserfs_xattr_header *rxh;
  589. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  590. if (chunk + skip > PAGE_CACHE_SIZE)
  591. chunk = PAGE_CACHE_SIZE - skip;
  592. rxh = (struct reiserfs_xattr_header *)data;
  593. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  594. rxh->h_hash = cpu_to_le32(xahash);
  595. }
  596. err = reiserfs_prepare_write(NULL, page, page_offset,
  597. page_offset + chunk + skip);
  598. if (!err) {
  599. if (buffer)
  600. memcpy(data + skip, buffer + buffer_pos, chunk);
  601. err = reiserfs_commit_write(NULL, page, page_offset,
  602. page_offset + chunk +
  603. skip);
  604. }
  605. unlock_page(page);
  606. reiserfs_put_page(page);
  607. buffer_pos += chunk;
  608. file_pos += chunk;
  609. skip = 0;
  610. if (err || buffer_size == 0 || !buffer)
  611. break;
  612. }
  613. /* We can't mark the inode dirty if it's not hashed. This is the case
  614. * when we're inheriting the default ACL. If we dirty it, the inode
  615. * gets marked dirty, but won't (ever) make it onto the dirty list until
  616. * it's synced explicitly to clear I_DIRTY. This is bad. */
  617. if (!hlist_unhashed(&inode->i_hash)) {
  618. inode->i_ctime = CURRENT_TIME_SEC;
  619. mark_inode_dirty(inode);
  620. }
  621. out_filp:
  622. up_write(&REISERFS_I(inode)->i_xattr_sem);
  623. dput(dentry);
  624. out:
  625. return err;
  626. }
  627. /*
  628. * inode->i_mutex: down
  629. */
  630. int
  631. reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
  632. size_t buffer_size)
  633. {
  634. ssize_t err = 0;
  635. struct dentry *dentry;
  636. size_t isize;
  637. size_t file_pos = 0;
  638. size_t buffer_pos = 0;
  639. struct page *page;
  640. __u32 hash = 0;
  641. if (name == NULL)
  642. return -EINVAL;
  643. /* We can't have xattrs attached to v1 items since they don't have
  644. * generation numbers */
  645. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  646. return -EOPNOTSUPP;
  647. dentry = get_xa_file_dentry(inode, name, XATTR_REPLACE);
  648. if (IS_ERR(dentry)) {
  649. err = PTR_ERR(dentry);
  650. goto out;
  651. }
  652. down_read(&REISERFS_I(inode)->i_xattr_sem);
  653. isize = i_size_read(dentry->d_inode);
  654. /* Just return the size needed */
  655. if (buffer == NULL) {
  656. err = isize - sizeof(struct reiserfs_xattr_header);
  657. goto out_unlock;
  658. }
  659. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  660. err = -ERANGE;
  661. goto out_unlock;
  662. }
  663. while (file_pos < isize) {
  664. size_t chunk;
  665. char *data;
  666. size_t skip = 0;
  667. if (isize - file_pos > PAGE_CACHE_SIZE)
  668. chunk = PAGE_CACHE_SIZE;
  669. else
  670. chunk = isize - file_pos;
  671. page = reiserfs_get_page(dentry->d_inode, file_pos);
  672. if (IS_ERR(page)) {
  673. err = PTR_ERR(page);
  674. goto out_unlock;
  675. }
  676. lock_page(page);
  677. data = page_address(page);
  678. if (file_pos == 0) {
  679. struct reiserfs_xattr_header *rxh =
  680. (struct reiserfs_xattr_header *)data;
  681. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  682. chunk -= skip;
  683. /* Magic doesn't match up.. */
  684. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  685. unlock_page(page);
  686. reiserfs_put_page(page);
  687. reiserfs_warning(inode->i_sb, "jdm-20001",
  688. "Invalid magic for xattr (%s) "
  689. "associated with %k", name,
  690. INODE_PKEY(inode));
  691. err = -EIO;
  692. goto out_unlock;
  693. }
  694. hash = le32_to_cpu(rxh->h_hash);
  695. }
  696. memcpy(buffer + buffer_pos, data + skip, chunk);
  697. unlock_page(page);
  698. reiserfs_put_page(page);
  699. file_pos += chunk;
  700. buffer_pos += chunk;
  701. skip = 0;
  702. }
  703. err = isize - sizeof(struct reiserfs_xattr_header);
  704. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  705. hash) {
  706. reiserfs_warning(inode->i_sb, "jdm-20002",
  707. "Invalid hash for xattr (%s) associated "
  708. "with %k", name, INODE_PKEY(inode));
  709. err = -EIO;
  710. }
  711. out_unlock:
  712. up_read(&REISERFS_I(inode)->i_xattr_sem);
  713. dput(dentry);
  714. out:
  715. return err;
  716. }
  717. int reiserfs_xattr_del(struct inode *inode, const char *name)
  718. {
  719. struct dentry *dir;
  720. int err;
  721. dir = open_xa_dir(inode, XATTR_REPLACE);
  722. if (IS_ERR(dir)) {
  723. err = PTR_ERR(dir);
  724. goto out;
  725. }
  726. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
  727. err = __reiserfs_xattr_del(dir, name, strlen(name));
  728. mutex_unlock(&dir->d_inode->i_mutex);
  729. dput(dir);
  730. if (!err) {
  731. inode->i_ctime = CURRENT_TIME_SEC;
  732. mark_inode_dirty(inode);
  733. }
  734. out:
  735. return err;
  736. }
  737. /* Actual operations that are exported to VFS-land */
  738. /*
  739. * Inode operation getxattr()
  740. */
  741. ssize_t
  742. reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
  743. size_t size)
  744. {
  745. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  746. int err;
  747. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  748. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  749. return -EOPNOTSUPP;
  750. err = xah->get(dentry->d_inode, name, buffer, size);
  751. return err;
  752. }
  753. /*
  754. * Inode operation setxattr()
  755. *
  756. * dentry->d_inode->i_mutex down
  757. */
  758. int
  759. reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  760. size_t size, int flags)
  761. {
  762. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  763. int err;
  764. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  765. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  766. return -EOPNOTSUPP;
  767. err = xah->set(dentry->d_inode, name, value, size, flags);
  768. return err;
  769. }
  770. /*
  771. * Inode operation removexattr()
  772. *
  773. * dentry->d_inode->i_mutex down
  774. */
  775. int reiserfs_removexattr(struct dentry *dentry, const char *name)
  776. {
  777. int err;
  778. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  779. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  780. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  781. return -EOPNOTSUPP;
  782. err = reiserfs_xattr_del(dentry->d_inode, name);
  783. dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
  784. mark_inode_dirty(dentry->d_inode);
  785. return err;
  786. }
  787. /* This is what filldir will use:
  788. * r_pos will always contain the amount of space required for the entire
  789. * list. If r_pos becomes larger than r_size, we need more space and we
  790. * return an error indicating this. If r_pos is less than r_size, then we've
  791. * filled the buffer successfully and we return success */
  792. struct reiserfs_listxattr_buf {
  793. int r_pos;
  794. int r_size;
  795. char *r_buf;
  796. struct inode *r_inode;
  797. };
  798. static int
  799. reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
  800. loff_t offset, u64 ino, unsigned int d_type)
  801. {
  802. struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
  803. int len = 0;
  804. if (name[0] != '.'
  805. || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  806. struct reiserfs_xattr_handler *xah =
  807. find_xattr_handler_prefix(name);
  808. if (!xah)
  809. return 0; /* Unsupported xattr name, skip it */
  810. /* We call ->list() twice because the operation isn't required to just
  811. * return the name back - we want to make sure we have enough space */
  812. len += xah->list(b->r_inode, name, namelen, NULL);
  813. if (len) {
  814. if (b->r_pos + len + 1 <= b->r_size) {
  815. char *p = b->r_buf + b->r_pos;
  816. p += xah->list(b->r_inode, name, namelen, p);
  817. *p++ = '\0';
  818. }
  819. b->r_pos += len + 1;
  820. }
  821. }
  822. return 0;
  823. }
  824. /*
  825. * Inode operation listxattr()
  826. */
  827. ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
  828. {
  829. struct dentry *dir;
  830. int err = 0;
  831. struct reiserfs_listxattr_buf buf;
  832. if (!dentry->d_inode)
  833. return -EINVAL;
  834. if (!reiserfs_xattrs(dentry->d_sb) ||
  835. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  836. return -EOPNOTSUPP;
  837. dir = open_xa_dir(dentry->d_inode, XATTR_REPLACE);
  838. if (IS_ERR(dir)) {
  839. err = PTR_ERR(dir);
  840. if (err == -ENODATA)
  841. err = 0; /* Not an error if there aren't any xattrs */
  842. goto out;
  843. }
  844. buf.r_buf = buffer;
  845. buf.r_size = buffer ? size : 0;
  846. buf.r_pos = 0;
  847. buf.r_inode = dentry->d_inode;
  848. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
  849. err = xattr_readdir(dir->d_inode, reiserfs_listxattr_filler, &buf);
  850. mutex_unlock(&dir->d_inode->i_mutex);
  851. if (!err) {
  852. if (buf.r_pos > buf.r_size && buffer != NULL)
  853. err = -ERANGE;
  854. else
  855. err = buf.r_pos;
  856. }
  857. dput(dir);
  858. out:
  859. return err;
  860. }
  861. /* This is the implementation for the xattr plugin infrastructure */
  862. static LIST_HEAD(xattr_handlers);
  863. static DEFINE_RWLOCK(handler_lock);
  864. static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
  865. *prefix)
  866. {
  867. struct reiserfs_xattr_handler *xah = NULL;
  868. struct list_head *p;
  869. read_lock(&handler_lock);
  870. list_for_each(p, &xattr_handlers) {
  871. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  872. if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
  873. break;
  874. xah = NULL;
  875. }
  876. read_unlock(&handler_lock);
  877. return xah;
  878. }
  879. static void __unregister_handlers(void)
  880. {
  881. struct reiserfs_xattr_handler *xah;
  882. struct list_head *p, *tmp;
  883. list_for_each_safe(p, tmp, &xattr_handlers) {
  884. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  885. if (xah->exit)
  886. xah->exit();
  887. list_del_init(p);
  888. }
  889. INIT_LIST_HEAD(&xattr_handlers);
  890. }
  891. int __init reiserfs_xattr_register_handlers(void)
  892. {
  893. int err = 0;
  894. struct reiserfs_xattr_handler *xah;
  895. struct list_head *p;
  896. write_lock(&handler_lock);
  897. /* If we're already initialized, nothing to do */
  898. if (!list_empty(&xattr_handlers)) {
  899. write_unlock(&handler_lock);
  900. return 0;
  901. }
  902. /* Add the handlers */
  903. list_add_tail(&user_handler.handlers, &xattr_handlers);
  904. list_add_tail(&trusted_handler.handlers, &xattr_handlers);
  905. #ifdef CONFIG_REISERFS_FS_SECURITY
  906. list_add_tail(&security_handler.handlers, &xattr_handlers);
  907. #endif
  908. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  909. list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
  910. list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
  911. #endif
  912. /* Run initializers, if available */
  913. list_for_each(p, &xattr_handlers) {
  914. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  915. if (xah->init) {
  916. err = xah->init();
  917. if (err) {
  918. list_del_init(p);
  919. break;
  920. }
  921. }
  922. }
  923. /* Clean up other handlers, if any failed */
  924. if (err)
  925. __unregister_handlers();
  926. write_unlock(&handler_lock);
  927. return err;
  928. }
  929. void reiserfs_xattr_unregister_handlers(void)
  930. {
  931. write_lock(&handler_lock);
  932. __unregister_handlers();
  933. write_unlock(&handler_lock);
  934. }
  935. static int reiserfs_check_acl(struct inode *inode, int mask)
  936. {
  937. struct posix_acl *acl;
  938. int error = -EAGAIN; /* do regular unix permission checks by default */
  939. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  940. if (acl) {
  941. if (!IS_ERR(acl)) {
  942. error = posix_acl_permission(inode, acl, mask);
  943. posix_acl_release(acl);
  944. } else if (PTR_ERR(acl) != -ENODATA)
  945. error = PTR_ERR(acl);
  946. }
  947. return error;
  948. }
  949. int reiserfs_permission(struct inode *inode, int mask)
  950. {
  951. /*
  952. * We don't do permission checks on the internal objects.
  953. * Permissions are determined by the "owning" object.
  954. */
  955. if (IS_PRIVATE(inode))
  956. return 0;
  957. /*
  958. * Stat data v1 doesn't support ACLs.
  959. */
  960. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  961. return generic_permission(inode, mask, NULL);
  962. else
  963. return generic_permission(inode, mask, reiserfs_check_acl);
  964. }
  965. static int create_privroot(struct dentry *dentry)
  966. {
  967. int err;
  968. struct inode *inode = dentry->d_parent->d_inode;
  969. mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
  970. err = xattr_mkdir(inode, dentry, 0700);
  971. mutex_unlock(&inode->i_mutex);
  972. if (err) {
  973. dput(dentry);
  974. dentry = NULL;
  975. }
  976. if (dentry && dentry->d_inode)
  977. reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
  978. "storage.\n", PRIVROOT_NAME);
  979. return err;
  980. }
  981. static int xattr_mount_check(struct super_block *s)
  982. {
  983. /* We need generation numbers to ensure that the oid mapping is correct
  984. * v3.5 filesystems don't have them. */
  985. if (!old_format_only(s)) {
  986. set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  987. } else if (reiserfs_xattrs_optional(s)) {
  988. /* Old format filesystem, but optional xattrs have been enabled
  989. * at mount time. Error out. */
  990. reiserfs_warning(s, "jdm-20005",
  991. "xattrs/ACLs not supported on pre v3.6 "
  992. "format filesystem. Failing mount.");
  993. return -EOPNOTSUPP;
  994. } else {
  995. /* Old format filesystem, but no optional xattrs have
  996. * been enabled. This means we silently disable xattrs
  997. * on the filesystem. */
  998. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  999. }
  1000. return 0;
  1001. }
  1002. #else
  1003. int __init reiserfs_xattr_register_handlers(void) { return 0; }
  1004. void reiserfs_xattr_unregister_handlers(void) {}
  1005. #endif
  1006. /* This will catch lookups from the fs root to .reiserfs_priv */
  1007. static int
  1008. xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
  1009. {
  1010. struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
  1011. if (name->len == priv_root->d_name.len &&
  1012. name->hash == priv_root->d_name.hash &&
  1013. !memcmp(name->name, priv_root->d_name.name, name->len)) {
  1014. return -ENOENT;
  1015. } else if (q1->len == name->len &&
  1016. !memcmp(q1->name, name->name, name->len))
  1017. return 0;
  1018. return 1;
  1019. }
  1020. static struct dentry_operations xattr_lookup_poison_ops = {
  1021. .d_compare = xattr_lookup_poison,
  1022. };
  1023. /* We need to take a copy of the mount flags since things like
  1024. * MS_RDONLY don't get set until *after* we're called.
  1025. * mount_flags != mount_options */
  1026. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  1027. {
  1028. int err = 0;
  1029. #ifdef CONFIG_REISERFS_FS_XATTR
  1030. err = xattr_mount_check(s);
  1031. if (err)
  1032. goto error;
  1033. #endif
  1034. /* If we don't have the privroot located yet - go find it */
  1035. if (!REISERFS_SB(s)->priv_root) {
  1036. struct dentry *dentry;
  1037. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  1038. strlen(PRIVROOT_NAME));
  1039. if (!IS_ERR(dentry)) {
  1040. #ifdef CONFIG_REISERFS_FS_XATTR
  1041. if (!(mount_flags & MS_RDONLY) && !dentry->d_inode)
  1042. err = create_privroot(dentry);
  1043. #endif
  1044. if (!dentry->d_inode) {
  1045. dput(dentry);
  1046. dentry = NULL;
  1047. }
  1048. } else
  1049. err = PTR_ERR(dentry);
  1050. if (!err && dentry) {
  1051. s->s_root->d_op = &xattr_lookup_poison_ops;
  1052. dentry->d_inode->i_flags |= S_PRIVATE;
  1053. REISERFS_SB(s)->priv_root = dentry;
  1054. #ifdef CONFIG_REISERFS_FS_XATTR
  1055. /* xattrs are unavailable */
  1056. } else if (!(mount_flags & MS_RDONLY)) {
  1057. /* If we're read-only it just means that the dir
  1058. * hasn't been created. Not an error -- just no
  1059. * xattrs on the fs. We'll check again if we
  1060. * go read-write */
  1061. reiserfs_warning(s, "jdm-20006",
  1062. "xattrs/ACLs enabled and couldn't "
  1063. "find/create .reiserfs_priv. "
  1064. "Failing mount.");
  1065. err = -EOPNOTSUPP;
  1066. #endif
  1067. }
  1068. }
  1069. #ifdef CONFIG_REISERFS_FS_XATTR
  1070. error:
  1071. if (err) {
  1072. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1073. clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
  1074. clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
  1075. }
  1076. #endif
  1077. /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
  1078. s->s_flags = s->s_flags & ~MS_POSIXACL;
  1079. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  1080. if (reiserfs_posixacl(s))
  1081. s->s_flags |= MS_POSIXACL;
  1082. #endif
  1083. return err;
  1084. }