xattr.c 36 KB

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