xattr.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  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. #include <linux/reiserfs_fs.h>
  31. #include <linux/dcache.h>
  32. #include <linux/namei.h>
  33. #include <linux/errno.h>
  34. #include <linux/fs.h>
  35. #include <linux/file.h>
  36. #include <linux/pagemap.h>
  37. #include <linux/xattr.h>
  38. #include <linux/reiserfs_xattr.h>
  39. #include <linux/reiserfs_acl.h>
  40. #include <linux/mbcache.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/checksum.h>
  43. #include <linux/smp_lock.h>
  44. #include <linux/stat.h>
  45. #include <asm/semaphore.h>
  46. #define FL_READONLY 128
  47. #define FL_DIR_SEM_HELD 256
  48. #define PRIVROOT_NAME ".reiserfs_priv"
  49. #define XAROOT_NAME "xattrs"
  50. static struct reiserfs_xattr_handler *find_xattr_handler_prefix (const char *prefix);
  51. static struct dentry *
  52. create_xa_root (struct super_block *sb)
  53. {
  54. struct dentry *privroot = dget (REISERFS_SB(sb)->priv_root);
  55. struct dentry *xaroot;
  56. /* This needs to be created at mount-time */
  57. if (!privroot)
  58. return ERR_PTR(-EOPNOTSUPP);
  59. xaroot = lookup_one_len (XAROOT_NAME, privroot, strlen (XAROOT_NAME));
  60. if (IS_ERR (xaroot)) {
  61. goto out;
  62. } else if (!xaroot->d_inode) {
  63. int err;
  64. down (&privroot->d_inode->i_sem);
  65. err = privroot->d_inode->i_op->mkdir (privroot->d_inode, xaroot, 0700);
  66. up (&privroot->d_inode->i_sem);
  67. if (err) {
  68. dput (xaroot);
  69. dput (privroot);
  70. return ERR_PTR (err);
  71. }
  72. REISERFS_SB(sb)->xattr_root = dget (xaroot);
  73. }
  74. out:
  75. dput (privroot);
  76. return xaroot;
  77. }
  78. /* This will return a dentry, or error, refering to the xa root directory.
  79. * If the xa root doesn't exist yet, the dentry will be returned without
  80. * an associated inode. This dentry can be used with ->mkdir to create
  81. * the xa directory. */
  82. static struct dentry *
  83. __get_xa_root (struct super_block *s)
  84. {
  85. struct dentry *privroot = dget (REISERFS_SB(s)->priv_root);
  86. struct dentry *xaroot = NULL;
  87. if (IS_ERR (privroot) || !privroot)
  88. return privroot;
  89. xaroot = lookup_one_len (XAROOT_NAME, privroot, strlen (XAROOT_NAME));
  90. if (IS_ERR (xaroot)) {
  91. goto out;
  92. } else if (!xaroot->d_inode) {
  93. dput (xaroot);
  94. xaroot = NULL;
  95. goto out;
  96. }
  97. REISERFS_SB(s)->xattr_root = dget (xaroot);
  98. out:
  99. dput (privroot);
  100. return xaroot;
  101. }
  102. /* Returns the dentry (or NULL) referring to the root of the extended
  103. * attribute directory tree. If it has already been retreived, it is used.
  104. * Otherwise, we attempt to retreive it from disk. It may also return
  105. * a pointer-encoded error.
  106. */
  107. static inline struct dentry *
  108. get_xa_root (struct super_block *s)
  109. {
  110. struct dentry *dentry = dget (REISERFS_SB(s)->xattr_root);
  111. if (!dentry)
  112. dentry = __get_xa_root (s);
  113. return dentry;
  114. }
  115. /* Opens the directory corresponding to the inode's extended attribute store.
  116. * If flags allow, the tree to the directory may be created. If creation is
  117. * prohibited, -ENODATA is returned. */
  118. static struct dentry *
  119. open_xa_dir (const struct inode *inode, int flags)
  120. {
  121. struct dentry *xaroot, *xadir;
  122. char namebuf[17];
  123. xaroot = get_xa_root (inode->i_sb);
  124. if (IS_ERR (xaroot)) {
  125. return xaroot;
  126. } else if (!xaroot) {
  127. if (flags == 0 || flags & XATTR_CREATE) {
  128. xaroot = create_xa_root (inode->i_sb);
  129. if (IS_ERR (xaroot))
  130. return xaroot;
  131. }
  132. if (!xaroot)
  133. return ERR_PTR (-ENODATA);
  134. }
  135. /* ok, we have xaroot open */
  136. snprintf (namebuf, sizeof (namebuf), "%X.%X",
  137. le32_to_cpu (INODE_PKEY (inode)->k_objectid),
  138. inode->i_generation);
  139. xadir = lookup_one_len (namebuf, xaroot, strlen (namebuf));
  140. if (IS_ERR (xadir)) {
  141. dput (xaroot);
  142. return xadir;
  143. }
  144. if (!xadir->d_inode) {
  145. int err;
  146. if (flags == 0 || flags & XATTR_CREATE) {
  147. /* Although there is nothing else trying to create this directory,
  148. * another directory with the same hash may be created, so we need
  149. * to protect against that */
  150. err = xaroot->d_inode->i_op->mkdir (xaroot->d_inode, xadir, 0700);
  151. if (err) {
  152. dput (xaroot);
  153. dput (xadir);
  154. return ERR_PTR (err);
  155. }
  156. }
  157. if (!xadir->d_inode) {
  158. dput (xaroot);
  159. dput (xadir);
  160. return ERR_PTR (-ENODATA);
  161. }
  162. }
  163. dput (xaroot);
  164. return xadir;
  165. }
  166. /* Returns a dentry corresponding to a specific extended attribute file
  167. * for the inode. If flags allow, the file is created. Otherwise, a
  168. * valid or negative dentry, or an error is returned. */
  169. static struct dentry *
  170. get_xa_file_dentry (const struct inode *inode, const char *name, int flags)
  171. {
  172. struct dentry *xadir, *xafile;
  173. int err = 0;
  174. xadir = open_xa_dir (inode, flags);
  175. if (IS_ERR (xadir)) {
  176. return ERR_PTR (PTR_ERR (xadir));
  177. } else if (xadir && !xadir->d_inode) {
  178. dput (xadir);
  179. return ERR_PTR (-ENODATA);
  180. }
  181. xafile = lookup_one_len (name, xadir, strlen (name));
  182. if (IS_ERR (xafile)) {
  183. dput (xadir);
  184. return ERR_PTR (PTR_ERR (xafile));
  185. }
  186. if (xafile->d_inode) { /* file exists */
  187. if (flags & XATTR_CREATE) {
  188. err = -EEXIST;
  189. dput (xafile);
  190. goto out;
  191. }
  192. } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
  193. goto out;
  194. } else {
  195. /* inode->i_sem is down, so nothing else can try to create
  196. * the same xattr */
  197. err = xadir->d_inode->i_op->create (xadir->d_inode, xafile,
  198. 0700|S_IFREG, NULL);
  199. if (err) {
  200. dput (xafile);
  201. goto out;
  202. }
  203. }
  204. out:
  205. dput (xadir);
  206. if (err)
  207. xafile = ERR_PTR (err);
  208. return xafile;
  209. }
  210. /* Opens a file pointer to the attribute associated with inode */
  211. static struct file *
  212. open_xa_file (const struct inode *inode, const char *name, int flags)
  213. {
  214. struct dentry *xafile;
  215. struct file *fp;
  216. xafile = get_xa_file_dentry (inode, name, flags);
  217. if (IS_ERR (xafile))
  218. return ERR_PTR (PTR_ERR (xafile));
  219. else if (!xafile->d_inode) {
  220. dput (xafile);
  221. return ERR_PTR (-ENODATA);
  222. }
  223. fp = dentry_open (xafile, NULL, O_RDWR);
  224. /* dentry_open dputs the dentry if it fails */
  225. return fp;
  226. }
  227. /*
  228. * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
  229. * we need to drop the path before calling the filldir struct. That
  230. * would be a big performance hit to the non-xattr case, so I've copied
  231. * the whole thing for now. --clm
  232. *
  233. * the big difference is that I go backwards through the directory,
  234. * and don't mess with f->f_pos, but the idea is the same. Do some
  235. * action on each and every entry in the directory.
  236. *
  237. * we're called with i_sem held, so there are no worries about the directory
  238. * changing underneath us.
  239. */
  240. static int __xattr_readdir(struct file * filp, void * dirent, filldir_t filldir)
  241. {
  242. struct inode *inode = filp->f_dentry->d_inode;
  243. struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
  244. INITIALIZE_PATH (path_to_entry);
  245. struct buffer_head * bh;
  246. int entry_num;
  247. struct item_head * ih, tmp_ih;
  248. int search_res;
  249. char * local_buf;
  250. loff_t next_pos;
  251. char small_buf[32] ; /* avoid kmalloc if we can */
  252. struct reiserfs_de_head *deh;
  253. int d_reclen;
  254. char * d_name;
  255. off_t d_off;
  256. ino_t d_ino;
  257. struct reiserfs_dir_entry de;
  258. /* form key for search the next directory entry using f_pos field of
  259. file structure */
  260. next_pos = max_reiserfs_offset(inode);
  261. while (1) {
  262. research:
  263. if (next_pos <= DOT_DOT_OFFSET)
  264. break;
  265. make_cpu_key (&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
  266. search_res = search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry, &de);
  267. if (search_res == IO_ERROR) {
  268. // FIXME: we could just skip part of directory which could
  269. // not be read
  270. pathrelse(&path_to_entry);
  271. return -EIO;
  272. }
  273. if (search_res == NAME_NOT_FOUND)
  274. de.de_entry_num--;
  275. set_de_name_and_namelen(&de);
  276. entry_num = de.de_entry_num;
  277. deh = &(de.de_deh[entry_num]);
  278. bh = de.de_bh;
  279. ih = de.de_ih;
  280. if (!is_direntry_le_ih(ih)) {
  281. reiserfs_warning(inode->i_sb, "not direntry %h", ih);
  282. break;
  283. }
  284. copy_item_head(&tmp_ih, ih);
  285. /* we must have found item, that is item of this directory, */
  286. RFALSE( COMP_SHORT_KEYS (&(ih->ih_key), &pos_key),
  287. "vs-9000: found item %h does not match to dir we readdir %K",
  288. ih, &pos_key);
  289. if (deh_offset(deh) <= DOT_DOT_OFFSET) {
  290. break;
  291. }
  292. /* look for the previous entry in the directory */
  293. next_pos = deh_offset (deh) - 1;
  294. if (!de_visible (deh))
  295. /* it is hidden entry */
  296. continue;
  297. d_reclen = entry_length(bh, ih, entry_num);
  298. d_name = B_I_DEH_ENTRY_FILE_NAME (bh, ih, deh);
  299. d_off = deh_offset (deh);
  300. d_ino = deh_objectid (deh);
  301. if (!d_name[d_reclen - 1])
  302. d_reclen = strlen (d_name);
  303. if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)){
  304. /* too big to send back to VFS */
  305. continue ;
  306. }
  307. /* Ignore the .reiserfs_priv entry */
  308. if (reiserfs_xattrs (inode->i_sb) &&
  309. !old_format_only(inode->i_sb) &&
  310. deh_objectid (deh) == le32_to_cpu (INODE_PKEY(REISERFS_SB(inode->i_sb)->priv_root->d_inode)->k_objectid))
  311. continue;
  312. if (d_reclen <= 32) {
  313. local_buf = small_buf ;
  314. } else {
  315. local_buf = reiserfs_kmalloc(d_reclen, GFP_NOFS, inode->i_sb) ;
  316. if (!local_buf) {
  317. pathrelse (&path_to_entry);
  318. return -ENOMEM ;
  319. }
  320. if (item_moved (&tmp_ih, &path_to_entry)) {
  321. reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
  322. /* sigh, must retry. Do this same offset again */
  323. next_pos = d_off;
  324. goto research;
  325. }
  326. }
  327. // Note, that we copy name to user space via temporary
  328. // buffer (local_buf) because filldir will block if
  329. // user space buffer is swapped out. At that time
  330. // entry can move to somewhere else
  331. memcpy (local_buf, d_name, d_reclen);
  332. /* the filldir function might need to start transactions,
  333. * or do who knows what. Release the path now that we've
  334. * copied all the important stuff out of the deh
  335. */
  336. pathrelse (&path_to_entry);
  337. if (filldir (dirent, local_buf, d_reclen, d_off, d_ino,
  338. DT_UNKNOWN) < 0) {
  339. if (local_buf != small_buf) {
  340. reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
  341. }
  342. goto end;
  343. }
  344. if (local_buf != small_buf) {
  345. reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
  346. }
  347. } /* while */
  348. end:
  349. pathrelse (&path_to_entry);
  350. return 0;
  351. }
  352. /*
  353. * this could be done with dedicated readdir ops for the xattr files,
  354. * but I want to get something working asap
  355. * this is stolen from vfs_readdir
  356. *
  357. */
  358. static
  359. int xattr_readdir(struct file *file, filldir_t filler, void *buf)
  360. {
  361. struct inode *inode = file->f_dentry->d_inode;
  362. int res = -ENOTDIR;
  363. if (!file->f_op || !file->f_op->readdir)
  364. goto out;
  365. down(&inode->i_sem);
  366. // down(&inode->i_zombie);
  367. res = -ENOENT;
  368. if (!IS_DEADDIR(inode)) {
  369. lock_kernel();
  370. res = __xattr_readdir(file, buf, filler);
  371. unlock_kernel();
  372. }
  373. // up(&inode->i_zombie);
  374. up(&inode->i_sem);
  375. out:
  376. return res;
  377. }
  378. /* Internal operations on file data */
  379. static inline void
  380. reiserfs_put_page(struct page *page)
  381. {
  382. kunmap(page);
  383. page_cache_release(page);
  384. }
  385. static struct page *
  386. reiserfs_get_page(struct inode *dir, unsigned long n)
  387. {
  388. struct address_space *mapping = dir->i_mapping;
  389. struct page *page;
  390. /* We can deadlock if we try to free dentries,
  391. and an unlink/rmdir has just occured - GFP_NOFS avoids this */
  392. mapping->flags = (mapping->flags & ~__GFP_BITS_MASK) | GFP_NOFS;
  393. page = read_cache_page (mapping, n,
  394. (filler_t*)mapping->a_ops->readpage, NULL);
  395. if (!IS_ERR(page)) {
  396. wait_on_page_locked(page);
  397. kmap(page);
  398. if (!PageUptodate(page))
  399. goto fail;
  400. if (PageError(page))
  401. goto fail;
  402. }
  403. return page;
  404. fail:
  405. reiserfs_put_page(page);
  406. return ERR_PTR(-EIO);
  407. }
  408. static inline __u32
  409. xattr_hash (const char *msg, int len)
  410. {
  411. return csum_partial (msg, len, 0);
  412. }
  413. /* Generic extended attribute operations that can be used by xa plugins */
  414. /*
  415. * inode->i_sem: down
  416. */
  417. int
  418. reiserfs_xattr_set (struct inode *inode, const char *name, const void *buffer,
  419. size_t buffer_size, int flags)
  420. {
  421. int err = 0;
  422. struct file *fp;
  423. struct page *page;
  424. char *data;
  425. struct address_space *mapping;
  426. size_t file_pos = 0;
  427. size_t buffer_pos = 0;
  428. struct inode *xinode;
  429. struct iattr newattrs;
  430. __u32 xahash = 0;
  431. if (IS_RDONLY (inode))
  432. return -EROFS;
  433. if (IS_IMMUTABLE (inode) || IS_APPEND (inode))
  434. return -EPERM;
  435. if (get_inode_sd_version (inode) == STAT_DATA_V1)
  436. return -EOPNOTSUPP;
  437. /* Empty xattrs are ok, they're just empty files, no hash */
  438. if (buffer && buffer_size)
  439. xahash = xattr_hash (buffer, buffer_size);
  440. open_file:
  441. fp = open_xa_file (inode, name, flags);
  442. if (IS_ERR (fp)) {
  443. err = PTR_ERR (fp);
  444. goto out;
  445. }
  446. xinode = fp->f_dentry->d_inode;
  447. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  448. /* we need to copy it off.. */
  449. if (xinode->i_nlink > 1) {
  450. fput(fp);
  451. err = reiserfs_xattr_del (inode, name);
  452. if (err < 0)
  453. goto out;
  454. /* We just killed the old one, we're not replacing anymore */
  455. if (flags & XATTR_REPLACE)
  456. flags &= ~XATTR_REPLACE;
  457. goto open_file;
  458. }
  459. /* Resize it so we're ok to write there */
  460. newattrs.ia_size = buffer_size;
  461. newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
  462. down (&xinode->i_sem);
  463. err = notify_change(fp->f_dentry, &newattrs);
  464. if (err)
  465. goto out_filp;
  466. mapping = xinode->i_mapping;
  467. while (buffer_pos < buffer_size || buffer_pos == 0) {
  468. size_t chunk;
  469. size_t skip = 0;
  470. size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
  471. if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
  472. chunk = PAGE_CACHE_SIZE;
  473. else
  474. chunk = buffer_size - buffer_pos;
  475. page = reiserfs_get_page (xinode, file_pos >> PAGE_CACHE_SHIFT);
  476. if (IS_ERR (page)) {
  477. err = PTR_ERR (page);
  478. goto out_filp;
  479. }
  480. lock_page (page);
  481. data = page_address (page);
  482. if (file_pos == 0) {
  483. struct reiserfs_xattr_header *rxh;
  484. skip = file_pos = sizeof (struct reiserfs_xattr_header);
  485. if (chunk + skip > PAGE_CACHE_SIZE)
  486. chunk = PAGE_CACHE_SIZE - skip;
  487. rxh = (struct reiserfs_xattr_header *)data;
  488. rxh->h_magic = cpu_to_le32 (REISERFS_XATTR_MAGIC);
  489. rxh->h_hash = cpu_to_le32 (xahash);
  490. }
  491. err = mapping->a_ops->prepare_write (fp, page, page_offset,
  492. page_offset + chunk + skip);
  493. if (!err) {
  494. if (buffer)
  495. memcpy (data + skip, buffer + buffer_pos, chunk);
  496. err = mapping->a_ops->commit_write (fp, page, page_offset,
  497. page_offset + chunk + skip);
  498. }
  499. unlock_page (page);
  500. reiserfs_put_page (page);
  501. buffer_pos += chunk;
  502. file_pos += chunk;
  503. skip = 0;
  504. if (err || buffer_size == 0 || !buffer)
  505. break;
  506. }
  507. /* We can't mark the inode dirty if it's not hashed. This is the case
  508. * when we're inheriting the default ACL. If we dirty it, the inode
  509. * gets marked dirty, but won't (ever) make it onto the dirty list until
  510. * it's synced explicitly to clear I_DIRTY. This is bad. */
  511. if (!hlist_unhashed(&inode->i_hash)) {
  512. inode->i_ctime = CURRENT_TIME_SEC;
  513. mark_inode_dirty (inode);
  514. }
  515. out_filp:
  516. up (&xinode->i_sem);
  517. fput(fp);
  518. out:
  519. return err;
  520. }
  521. /*
  522. * inode->i_sem: down
  523. */
  524. int
  525. reiserfs_xattr_get (const struct inode *inode, const char *name, void *buffer,
  526. size_t buffer_size)
  527. {
  528. ssize_t err = 0;
  529. struct file *fp;
  530. size_t isize;
  531. size_t file_pos = 0;
  532. size_t buffer_pos = 0;
  533. struct page *page;
  534. struct inode *xinode;
  535. __u32 hash = 0;
  536. if (name == NULL)
  537. return -EINVAL;
  538. /* We can't have xattrs attached to v1 items since they don't have
  539. * generation numbers */
  540. if (get_inode_sd_version (inode) == STAT_DATA_V1)
  541. return -EOPNOTSUPP;
  542. fp = open_xa_file (inode, name, FL_READONLY);
  543. if (IS_ERR (fp)) {
  544. err = PTR_ERR (fp);
  545. goto out;
  546. }
  547. xinode = fp->f_dentry->d_inode;
  548. isize = xinode->i_size;
  549. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  550. /* Just return the size needed */
  551. if (buffer == NULL) {
  552. err = isize - sizeof (struct reiserfs_xattr_header);
  553. goto out_dput;
  554. }
  555. if (buffer_size < isize - sizeof (struct reiserfs_xattr_header)) {
  556. err = -ERANGE;
  557. goto out_dput;
  558. }
  559. while (file_pos < isize) {
  560. size_t chunk;
  561. char *data;
  562. size_t skip = 0;
  563. if (isize - file_pos > PAGE_CACHE_SIZE)
  564. chunk = PAGE_CACHE_SIZE;
  565. else
  566. chunk = isize - file_pos;
  567. page = reiserfs_get_page (xinode, file_pos >> PAGE_CACHE_SHIFT);
  568. if (IS_ERR (page)) {
  569. err = PTR_ERR (page);
  570. goto out_dput;
  571. }
  572. lock_page (page);
  573. data = page_address (page);
  574. if (file_pos == 0) {
  575. struct reiserfs_xattr_header *rxh =
  576. (struct reiserfs_xattr_header *)data;
  577. skip = file_pos = sizeof (struct reiserfs_xattr_header);
  578. chunk -= skip;
  579. /* Magic doesn't match up.. */
  580. if (rxh->h_magic != cpu_to_le32 (REISERFS_XATTR_MAGIC)) {
  581. unlock_page (page);
  582. reiserfs_put_page (page);
  583. reiserfs_warning (inode->i_sb, "Invalid magic for xattr (%s) "
  584. "associated with %k", name,
  585. INODE_PKEY (inode));
  586. err = -EIO;
  587. goto out_dput;
  588. }
  589. hash = le32_to_cpu (rxh->h_hash);
  590. }
  591. memcpy (buffer + buffer_pos, data + skip, chunk);
  592. unlock_page (page);
  593. reiserfs_put_page (page);
  594. file_pos += chunk;
  595. buffer_pos += chunk;
  596. skip = 0;
  597. }
  598. err = isize - sizeof (struct reiserfs_xattr_header);
  599. if (xattr_hash (buffer, isize - sizeof (struct reiserfs_xattr_header)) != hash) {
  600. reiserfs_warning (inode->i_sb, "Invalid hash for xattr (%s) associated "
  601. "with %k", name, INODE_PKEY (inode));
  602. err = -EIO;
  603. }
  604. out_dput:
  605. fput(fp);
  606. out:
  607. return err;
  608. }
  609. static int
  610. __reiserfs_xattr_del (struct dentry *xadir, const char *name, int namelen)
  611. {
  612. struct dentry *dentry;
  613. struct inode *dir = xadir->d_inode;
  614. int err = 0;
  615. dentry = lookup_one_len (name, xadir, namelen);
  616. if (IS_ERR (dentry)) {
  617. err = PTR_ERR (dentry);
  618. goto out;
  619. } else if (!dentry->d_inode) {
  620. err = -ENODATA;
  621. goto out_file;
  622. }
  623. /* Skip directories.. */
  624. if (S_ISDIR (dentry->d_inode->i_mode))
  625. goto out_file;
  626. if (!is_reiserfs_priv_object (dentry->d_inode)) {
  627. reiserfs_warning (dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
  628. "priv flag set [parent is %sset].",
  629. le32_to_cpu (INODE_PKEY (dentry->d_inode)->k_objectid),
  630. xadir->d_name.len, xadir->d_name.name, namelen, name,
  631. is_reiserfs_priv_object (xadir->d_inode) ? "" : "not ");
  632. dput (dentry);
  633. return -EIO;
  634. }
  635. err = dir->i_op->unlink (dir, dentry);
  636. if (!err)
  637. d_delete (dentry);
  638. out_file:
  639. dput (dentry);
  640. out:
  641. return err;
  642. }
  643. int
  644. reiserfs_xattr_del (struct inode *inode, const char *name)
  645. {
  646. struct dentry *dir;
  647. int err;
  648. if (IS_RDONLY (inode))
  649. return -EROFS;
  650. dir = open_xa_dir (inode, FL_READONLY);
  651. if (IS_ERR (dir)) {
  652. err = PTR_ERR (dir);
  653. goto out;
  654. }
  655. err = __reiserfs_xattr_del (dir, name, strlen (name));
  656. dput (dir);
  657. if (!err) {
  658. inode->i_ctime = CURRENT_TIME_SEC;
  659. mark_inode_dirty (inode);
  660. }
  661. out:
  662. return err;
  663. }
  664. /* The following are side effects of other operations that aren't explicitly
  665. * modifying extended attributes. This includes operations such as permissions
  666. * or ownership changes, object deletions, etc. */
  667. static int
  668. reiserfs_delete_xattrs_filler (void *buf, const char *name, int namelen,
  669. loff_t offset, ino_t ino, unsigned int d_type)
  670. {
  671. struct dentry *xadir = (struct dentry *)buf;
  672. return __reiserfs_xattr_del (xadir, name, namelen);
  673. }
  674. /* This is called w/ inode->i_sem downed */
  675. int
  676. reiserfs_delete_xattrs (struct inode *inode)
  677. {
  678. struct file *fp;
  679. struct dentry *dir, *root;
  680. int err = 0;
  681. /* Skip out, an xattr has no xattrs associated with it */
  682. if (is_reiserfs_priv_object (inode) ||
  683. get_inode_sd_version (inode) == STAT_DATA_V1 ||
  684. !reiserfs_xattrs(inode->i_sb))
  685. {
  686. return 0;
  687. }
  688. reiserfs_read_lock_xattrs (inode->i_sb);
  689. dir = open_xa_dir (inode, FL_READONLY);
  690. reiserfs_read_unlock_xattrs (inode->i_sb);
  691. if (IS_ERR (dir)) {
  692. err = PTR_ERR (dir);
  693. goto out;
  694. } else if (!dir->d_inode) {
  695. dput (dir);
  696. return 0;
  697. }
  698. fp = dentry_open (dir, NULL, O_RDWR);
  699. if (IS_ERR (fp)) {
  700. err = PTR_ERR (fp);
  701. /* dentry_open dputs the dentry if it fails */
  702. goto out;
  703. }
  704. lock_kernel ();
  705. err = xattr_readdir (fp, reiserfs_delete_xattrs_filler, dir);
  706. if (err) {
  707. unlock_kernel ();
  708. goto out_dir;
  709. }
  710. /* Leftovers besides . and .. -- that's not good. */
  711. if (dir->d_inode->i_nlink <= 2) {
  712. root = get_xa_root (inode->i_sb);
  713. reiserfs_write_lock_xattrs (inode->i_sb);
  714. err = vfs_rmdir (root->d_inode, dir);
  715. reiserfs_write_unlock_xattrs (inode->i_sb);
  716. dput (root);
  717. } else {
  718. reiserfs_warning (inode->i_sb,
  719. "Couldn't remove all entries in directory");
  720. }
  721. unlock_kernel ();
  722. out_dir:
  723. fput(fp);
  724. out:
  725. if (!err)
  726. REISERFS_I(inode)->i_flags = REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
  727. return err;
  728. }
  729. struct reiserfs_chown_buf {
  730. struct inode *inode;
  731. struct dentry *xadir;
  732. struct iattr *attrs;
  733. };
  734. /* XXX: If there is a better way to do this, I'd love to hear about it */
  735. static int
  736. reiserfs_chown_xattrs_filler (void *buf, const char *name, int namelen,
  737. loff_t offset, ino_t ino, unsigned int d_type)
  738. {
  739. struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
  740. struct dentry *xafile, *xadir = chown_buf->xadir;
  741. struct iattr *attrs = chown_buf->attrs;
  742. int err = 0;
  743. xafile = lookup_one_len (name, xadir, namelen);
  744. if (IS_ERR (xafile))
  745. return PTR_ERR (xafile);
  746. else if (!xafile->d_inode) {
  747. dput (xafile);
  748. return -ENODATA;
  749. }
  750. if (!S_ISDIR (xafile->d_inode->i_mode))
  751. err = notify_change (xafile, attrs);
  752. dput (xafile);
  753. return err;
  754. }
  755. int
  756. reiserfs_chown_xattrs (struct inode *inode, struct iattr *attrs)
  757. {
  758. struct file *fp;
  759. struct dentry *dir;
  760. int err = 0;
  761. struct reiserfs_chown_buf buf;
  762. unsigned int ia_valid = attrs->ia_valid;
  763. /* Skip out, an xattr has no xattrs associated with it */
  764. if (is_reiserfs_priv_object (inode) ||
  765. get_inode_sd_version (inode) == STAT_DATA_V1 ||
  766. !reiserfs_xattrs(inode->i_sb))
  767. {
  768. return 0;
  769. }
  770. reiserfs_read_lock_xattrs (inode->i_sb);
  771. dir = open_xa_dir (inode, FL_READONLY);
  772. reiserfs_read_unlock_xattrs (inode->i_sb);
  773. if (IS_ERR (dir)) {
  774. if (PTR_ERR (dir) != -ENODATA)
  775. err = PTR_ERR (dir);
  776. goto out;
  777. } else if (!dir->d_inode) {
  778. dput (dir);
  779. goto out;
  780. }
  781. fp = dentry_open (dir, NULL, O_RDWR);
  782. if (IS_ERR (fp)) {
  783. err = PTR_ERR (fp);
  784. /* dentry_open dputs the dentry if it fails */
  785. goto out;
  786. }
  787. lock_kernel ();
  788. attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
  789. buf.xadir = dir;
  790. buf.attrs = attrs;
  791. buf.inode = inode;
  792. err = xattr_readdir (fp, reiserfs_chown_xattrs_filler, &buf);
  793. if (err) {
  794. unlock_kernel ();
  795. goto out_dir;
  796. }
  797. err = notify_change (dir, attrs);
  798. unlock_kernel ();
  799. out_dir:
  800. fput(fp);
  801. out:
  802. attrs->ia_valid = ia_valid;
  803. return err;
  804. }
  805. /* Actual operations that are exported to VFS-land */
  806. /*
  807. * Inode operation getxattr()
  808. * Preliminary locking: we down dentry->d_inode->i_sem
  809. */
  810. ssize_t
  811. reiserfs_getxattr (struct dentry *dentry, const char *name, void *buffer,
  812. size_t size)
  813. {
  814. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
  815. int err;
  816. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  817. get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
  818. return -EOPNOTSUPP;
  819. reiserfs_read_lock_xattr_i (dentry->d_inode);
  820. reiserfs_read_lock_xattrs (dentry->d_sb);
  821. err = xah->get (dentry->d_inode, name, buffer, size);
  822. reiserfs_read_unlock_xattrs (dentry->d_sb);
  823. reiserfs_read_unlock_xattr_i (dentry->d_inode);
  824. return err;
  825. }
  826. /*
  827. * Inode operation setxattr()
  828. *
  829. * dentry->d_inode->i_sem down
  830. */
  831. int
  832. reiserfs_setxattr (struct dentry *dentry, const char *name, const void *value,
  833. size_t size, int flags)
  834. {
  835. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
  836. int err;
  837. int lock;
  838. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  839. get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
  840. return -EOPNOTSUPP;
  841. if (IS_RDONLY (dentry->d_inode))
  842. return -EROFS;
  843. if (IS_IMMUTABLE (dentry->d_inode) || IS_APPEND (dentry->d_inode))
  844. return -EROFS;
  845. reiserfs_write_lock_xattr_i (dentry->d_inode);
  846. lock = !has_xattr_dir (dentry->d_inode);
  847. if (lock)
  848. reiserfs_write_lock_xattrs (dentry->d_sb);
  849. else
  850. reiserfs_read_lock_xattrs (dentry->d_sb);
  851. err = xah->set (dentry->d_inode, name, value, size, flags);
  852. if (lock)
  853. reiserfs_write_unlock_xattrs (dentry->d_sb);
  854. else
  855. reiserfs_read_unlock_xattrs (dentry->d_sb);
  856. reiserfs_write_unlock_xattr_i (dentry->d_inode);
  857. return err;
  858. }
  859. /*
  860. * Inode operation removexattr()
  861. *
  862. * dentry->d_inode->i_sem down
  863. */
  864. int
  865. reiserfs_removexattr (struct dentry *dentry, const char *name)
  866. {
  867. int err;
  868. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
  869. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  870. get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
  871. return -EOPNOTSUPP;
  872. if (IS_RDONLY (dentry->d_inode))
  873. return -EROFS;
  874. if (IS_IMMUTABLE (dentry->d_inode) || IS_APPEND (dentry->d_inode))
  875. return -EPERM;
  876. reiserfs_write_lock_xattr_i (dentry->d_inode);
  877. reiserfs_read_lock_xattrs (dentry->d_sb);
  878. /* Deletion pre-operation */
  879. if (xah->del) {
  880. err = xah->del (dentry->d_inode, name);
  881. if (err)
  882. goto out;
  883. }
  884. err = reiserfs_xattr_del (dentry->d_inode, name);
  885. dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
  886. mark_inode_dirty (dentry->d_inode);
  887. out:
  888. reiserfs_read_unlock_xattrs (dentry->d_sb);
  889. reiserfs_write_unlock_xattr_i (dentry->d_inode);
  890. return err;
  891. }
  892. /* This is what filldir will use:
  893. * r_pos will always contain the amount of space required for the entire
  894. * list. If r_pos becomes larger than r_size, we need more space and we
  895. * return an error indicating this. If r_pos is less than r_size, then we've
  896. * filled the buffer successfully and we return success */
  897. struct reiserfs_listxattr_buf {
  898. int r_pos;
  899. int r_size;
  900. char *r_buf;
  901. struct inode *r_inode;
  902. };
  903. static int
  904. reiserfs_listxattr_filler (void *buf, const char *name, int namelen,
  905. loff_t offset, ino_t ino, unsigned int d_type)
  906. {
  907. struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
  908. int len = 0;
  909. if (name[0] != '.' || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  910. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix (name);
  911. if (!xah) return 0; /* Unsupported xattr name, skip it */
  912. /* We call ->list() twice because the operation isn't required to just
  913. * return the name back - we want to make sure we have enough space */
  914. len += xah->list (b->r_inode, name, namelen, NULL);
  915. if (len) {
  916. if (b->r_pos + len + 1 <= b->r_size) {
  917. char *p = b->r_buf + b->r_pos;
  918. p += xah->list (b->r_inode, name, namelen, p);
  919. *p++ = '\0';
  920. }
  921. b->r_pos += len + 1;
  922. }
  923. }
  924. return 0;
  925. }
  926. /*
  927. * Inode operation listxattr()
  928. *
  929. * Preliminary locking: we down dentry->d_inode->i_sem
  930. */
  931. ssize_t
  932. reiserfs_listxattr (struct dentry *dentry, char *buffer, size_t size)
  933. {
  934. struct file *fp;
  935. struct dentry *dir;
  936. int err = 0;
  937. struct reiserfs_listxattr_buf buf;
  938. if (!dentry->d_inode)
  939. return -EINVAL;
  940. if (!reiserfs_xattrs(dentry->d_sb) ||
  941. get_inode_sd_version (dentry->d_inode) == STAT_DATA_V1)
  942. return -EOPNOTSUPP;
  943. reiserfs_read_lock_xattr_i (dentry->d_inode);
  944. reiserfs_read_lock_xattrs (dentry->d_sb);
  945. dir = open_xa_dir (dentry->d_inode, FL_READONLY);
  946. reiserfs_read_unlock_xattrs (dentry->d_sb);
  947. if (IS_ERR (dir)) {
  948. err = PTR_ERR (dir);
  949. if (err == -ENODATA)
  950. err = 0; /* Not an error if there aren't any xattrs */
  951. goto out;
  952. }
  953. fp = dentry_open (dir, NULL, O_RDWR);
  954. if (IS_ERR (fp)) {
  955. err = PTR_ERR (fp);
  956. /* dentry_open dputs the dentry if it fails */
  957. goto out;
  958. }
  959. buf.r_buf = buffer;
  960. buf.r_size = buffer ? size : 0;
  961. buf.r_pos = 0;
  962. buf.r_inode = dentry->d_inode;
  963. REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
  964. err = xattr_readdir (fp, reiserfs_listxattr_filler, &buf);
  965. if (err)
  966. goto out_dir;
  967. if (buf.r_pos > buf.r_size && buffer != NULL)
  968. err = -ERANGE;
  969. else
  970. err = buf.r_pos;
  971. out_dir:
  972. fput(fp);
  973. out:
  974. reiserfs_read_unlock_xattr_i (dentry->d_inode);
  975. return err;
  976. }
  977. /* This is the implementation for the xattr plugin infrastructure */
  978. static struct list_head xattr_handlers = LIST_HEAD_INIT (xattr_handlers);
  979. static DEFINE_RWLOCK(handler_lock);
  980. static struct reiserfs_xattr_handler *
  981. find_xattr_handler_prefix (const char *prefix)
  982. {
  983. struct reiserfs_xattr_handler *xah = NULL;
  984. struct list_head *p;
  985. read_lock (&handler_lock);
  986. list_for_each (p, &xattr_handlers) {
  987. xah = list_entry (p, struct reiserfs_xattr_handler, handlers);
  988. if (strncmp (xah->prefix, prefix, strlen (xah->prefix)) == 0)
  989. break;
  990. xah = NULL;
  991. }
  992. read_unlock (&handler_lock);
  993. return xah;
  994. }
  995. static void
  996. __unregister_handlers (void)
  997. {
  998. struct reiserfs_xattr_handler *xah;
  999. struct list_head *p, *tmp;
  1000. list_for_each_safe (p, tmp, &xattr_handlers) {
  1001. xah = list_entry (p, struct reiserfs_xattr_handler, handlers);
  1002. if (xah->exit)
  1003. xah->exit();
  1004. list_del_init (p);
  1005. }
  1006. INIT_LIST_HEAD (&xattr_handlers);
  1007. }
  1008. int __init
  1009. reiserfs_xattr_register_handlers (void)
  1010. {
  1011. int err = 0;
  1012. struct reiserfs_xattr_handler *xah;
  1013. struct list_head *p;
  1014. write_lock (&handler_lock);
  1015. /* If we're already initialized, nothing to do */
  1016. if (!list_empty (&xattr_handlers)) {
  1017. write_unlock (&handler_lock);
  1018. return 0;
  1019. }
  1020. /* Add the handlers */
  1021. list_add_tail (&user_handler.handlers, &xattr_handlers);
  1022. list_add_tail (&trusted_handler.handlers, &xattr_handlers);
  1023. #ifdef CONFIG_REISERFS_FS_SECURITY
  1024. list_add_tail (&security_handler.handlers, &xattr_handlers);
  1025. #endif
  1026. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  1027. list_add_tail (&posix_acl_access_handler.handlers, &xattr_handlers);
  1028. list_add_tail (&posix_acl_default_handler.handlers, &xattr_handlers);
  1029. #endif
  1030. /* Run initializers, if available */
  1031. list_for_each (p, &xattr_handlers) {
  1032. xah = list_entry (p, struct reiserfs_xattr_handler, handlers);
  1033. if (xah->init) {
  1034. err = xah->init ();
  1035. if (err) {
  1036. list_del_init (p);
  1037. break;
  1038. }
  1039. }
  1040. }
  1041. /* Clean up other handlers, if any failed */
  1042. if (err)
  1043. __unregister_handlers ();
  1044. write_unlock (&handler_lock);
  1045. return err;
  1046. }
  1047. void
  1048. reiserfs_xattr_unregister_handlers (void)
  1049. {
  1050. write_lock (&handler_lock);
  1051. __unregister_handlers ();
  1052. write_unlock (&handler_lock);
  1053. }
  1054. /* This will catch lookups from the fs root to .reiserfs_priv */
  1055. static int
  1056. xattr_lookup_poison (struct dentry *dentry, struct qstr *q1, struct qstr *name)
  1057. {
  1058. struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
  1059. if (name->len == priv_root->d_name.len &&
  1060. name->hash == priv_root->d_name.hash &&
  1061. !memcmp (name->name, priv_root->d_name.name, name->len)) {
  1062. return -ENOENT;
  1063. } else if (q1->len == name->len &&
  1064. !memcmp(q1->name, name->name, name->len))
  1065. return 0;
  1066. return 1;
  1067. }
  1068. static struct dentry_operations xattr_lookup_poison_ops = {
  1069. .d_compare = xattr_lookup_poison,
  1070. };
  1071. /* We need to take a copy of the mount flags since things like
  1072. * MS_RDONLY don't get set until *after* we're called.
  1073. * mount_flags != mount_options */
  1074. int
  1075. reiserfs_xattr_init (struct super_block *s, int mount_flags)
  1076. {
  1077. int err = 0;
  1078. /* We need generation numbers to ensure that the oid mapping is correct
  1079. * v3.5 filesystems don't have them. */
  1080. if (!old_format_only (s)) {
  1081. set_bit (REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1082. } else if (reiserfs_xattrs_optional (s)) {
  1083. /* Old format filesystem, but optional xattrs have been enabled
  1084. * at mount time. Error out. */
  1085. reiserfs_warning (s, "xattrs/ACLs not supported on pre v3.6 "
  1086. "format filesystem. Failing mount.");
  1087. err = -EOPNOTSUPP;
  1088. goto error;
  1089. } else {
  1090. /* Old format filesystem, but no optional xattrs have been enabled. This
  1091. * means we silently disable xattrs on the filesystem. */
  1092. clear_bit (REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1093. }
  1094. /* If we don't have the privroot located yet - go find it */
  1095. if (reiserfs_xattrs (s) && !REISERFS_SB(s)->priv_root) {
  1096. struct dentry *dentry;
  1097. dentry = lookup_one_len (PRIVROOT_NAME, s->s_root,
  1098. strlen (PRIVROOT_NAME));
  1099. if (!IS_ERR (dentry)) {
  1100. if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
  1101. struct inode *inode = dentry->d_parent->d_inode;
  1102. down (&inode->i_sem);
  1103. err = inode->i_op->mkdir (inode, dentry, 0700);
  1104. up (&inode->i_sem);
  1105. if (err) {
  1106. dput (dentry);
  1107. dentry = NULL;
  1108. }
  1109. if (dentry && dentry->d_inode)
  1110. reiserfs_warning (s, "Created %s on %s - reserved for "
  1111. "xattr storage.", PRIVROOT_NAME,
  1112. reiserfs_bdevname (inode->i_sb));
  1113. } else if (!dentry->d_inode) {
  1114. dput (dentry);
  1115. dentry = NULL;
  1116. }
  1117. } else
  1118. err = PTR_ERR (dentry);
  1119. if (!err && dentry) {
  1120. s->s_root->d_op = &xattr_lookup_poison_ops;
  1121. reiserfs_mark_inode_private (dentry->d_inode);
  1122. REISERFS_SB(s)->priv_root = dentry;
  1123. } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
  1124. /* If we're read-only it just means that the dir hasn't been
  1125. * created. Not an error -- just no xattrs on the fs. We'll
  1126. * check again if we go read-write */
  1127. reiserfs_warning (s, "xattrs/ACLs enabled and couldn't "
  1128. "find/create .reiserfs_priv. Failing mount.");
  1129. err = -EOPNOTSUPP;
  1130. }
  1131. }
  1132. error:
  1133. /* This is only nonzero if there was an error initializing the xattr
  1134. * directory or if there is a condition where we don't support them. */
  1135. if (err) {
  1136. clear_bit (REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1137. clear_bit (REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
  1138. clear_bit (REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
  1139. }
  1140. /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
  1141. s->s_flags = s->s_flags & ~MS_POSIXACL;
  1142. if (reiserfs_posixacl (s))
  1143. s->s_flags |= MS_POSIXACL;
  1144. return err;
  1145. }
  1146. static int
  1147. __reiserfs_permission (struct inode *inode, int mask, struct nameidata *nd,
  1148. int need_lock)
  1149. {
  1150. umode_t mode = inode->i_mode;
  1151. if (mask & MAY_WRITE) {
  1152. /*
  1153. * Nobody gets write access to a read-only fs.
  1154. */
  1155. if (IS_RDONLY(inode) &&
  1156. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  1157. return -EROFS;
  1158. /*
  1159. * Nobody gets write access to an immutable file.
  1160. */
  1161. if (IS_IMMUTABLE(inode))
  1162. return -EACCES;
  1163. }
  1164. /* We don't do permission checks on the internal objects.
  1165. * Permissions are determined by the "owning" object. */
  1166. if (is_reiserfs_priv_object (inode))
  1167. return 0;
  1168. if (current->fsuid == inode->i_uid) {
  1169. mode >>= 6;
  1170. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  1171. } else if (reiserfs_posixacl(inode->i_sb) &&
  1172. get_inode_sd_version (inode) != STAT_DATA_V1) {
  1173. struct posix_acl *acl;
  1174. /* ACL can't contain additional permissions if
  1175. the ACL_MASK entry is 0 */
  1176. if (!(mode & S_IRWXG))
  1177. goto check_groups;
  1178. if (need_lock) {
  1179. reiserfs_read_lock_xattr_i (inode);
  1180. reiserfs_read_lock_xattrs (inode->i_sb);
  1181. }
  1182. acl = reiserfs_get_acl (inode, ACL_TYPE_ACCESS);
  1183. if (need_lock) {
  1184. reiserfs_read_unlock_xattrs (inode->i_sb);
  1185. reiserfs_read_unlock_xattr_i (inode);
  1186. }
  1187. if (IS_ERR (acl)) {
  1188. if (PTR_ERR (acl) == -ENODATA)
  1189. goto check_groups;
  1190. return PTR_ERR (acl);
  1191. }
  1192. if (acl) {
  1193. int err = posix_acl_permission (inode, acl, mask);
  1194. posix_acl_release (acl);
  1195. if (err == -EACCES) {
  1196. goto check_capabilities;
  1197. }
  1198. return err;
  1199. } else {
  1200. goto check_groups;
  1201. }
  1202. #endif
  1203. } else {
  1204. check_groups:
  1205. if (in_group_p(inode->i_gid))
  1206. mode >>= 3;
  1207. }
  1208. /*
  1209. * If the DACs are ok we don't need any capability check.
  1210. */
  1211. if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
  1212. return 0;
  1213. check_capabilities:
  1214. /*
  1215. * Read/write DACs are always overridable.
  1216. * Executable DACs are overridable if at least one exec bit is set.
  1217. */
  1218. if (!(mask & MAY_EXEC) ||
  1219. (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
  1220. if (capable(CAP_DAC_OVERRIDE))
  1221. return 0;
  1222. /*
  1223. * Searching includes executable on directories, else just read.
  1224. */
  1225. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  1226. if (capable(CAP_DAC_READ_SEARCH))
  1227. return 0;
  1228. return -EACCES;
  1229. }
  1230. int
  1231. reiserfs_permission (struct inode *inode, int mask, struct nameidata *nd)
  1232. {
  1233. return __reiserfs_permission (inode, mask, nd, 1);
  1234. }
  1235. int
  1236. reiserfs_permission_locked (struct inode *inode, int mask, struct nameidata *nd)
  1237. {
  1238. return __reiserfs_permission (inode, mask, nd, 0);
  1239. }