inode.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/posix_acl.h>
  15. #include <linux/sort.h>
  16. #include <linux/gfs2_ondisk.h>
  17. #include <linux/crc32.h>
  18. #include <linux/security.h>
  19. #include <linux/time.h>
  20. #include "gfs2.h"
  21. #include "incore.h"
  22. #include "acl.h"
  23. #include "bmap.h"
  24. #include "dir.h"
  25. #include "xattr.h"
  26. #include "glock.h"
  27. #include "glops.h"
  28. #include "inode.h"
  29. #include "log.h"
  30. #include "meta_io.h"
  31. #include "quota.h"
  32. #include "rgrp.h"
  33. #include "trans.h"
  34. #include "util.h"
  35. struct gfs2_inum_range_host {
  36. u64 ir_start;
  37. u64 ir_length;
  38. };
  39. static int iget_test(struct inode *inode, void *opaque)
  40. {
  41. struct gfs2_inode *ip = GFS2_I(inode);
  42. u64 *no_addr = opaque;
  43. if (ip->i_no_addr == *no_addr)
  44. return 1;
  45. return 0;
  46. }
  47. static int iget_set(struct inode *inode, void *opaque)
  48. {
  49. struct gfs2_inode *ip = GFS2_I(inode);
  50. u64 *no_addr = opaque;
  51. inode->i_ino = (unsigned long)*no_addr;
  52. ip->i_no_addr = *no_addr;
  53. return 0;
  54. }
  55. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
  56. {
  57. unsigned long hash = (unsigned long)no_addr;
  58. return ilookup5(sb, hash, iget_test, &no_addr);
  59. }
  60. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
  61. {
  62. unsigned long hash = (unsigned long)no_addr;
  63. return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
  64. }
  65. /**
  66. * GFS2 lookup code fills in vfs inode contents based on info obtained
  67. * from directory entry inside gfs2_inode_lookup(). This has caused issues
  68. * with NFS code path since its get_dentry routine doesn't have the relevant
  69. * directory entry when gfs2_inode_lookup() is invoked. Part of the code
  70. * segment inside gfs2_inode_lookup code needs to get moved around.
  71. *
  72. * Clears I_NEW as well.
  73. **/
  74. void gfs2_set_iop(struct inode *inode)
  75. {
  76. struct gfs2_sbd *sdp = GFS2_SB(inode);
  77. umode_t mode = inode->i_mode;
  78. if (S_ISREG(mode)) {
  79. inode->i_op = &gfs2_file_iops;
  80. if (gfs2_localflocks(sdp))
  81. inode->i_fop = &gfs2_file_fops_nolock;
  82. else
  83. inode->i_fop = &gfs2_file_fops;
  84. } else if (S_ISDIR(mode)) {
  85. inode->i_op = &gfs2_dir_iops;
  86. if (gfs2_localflocks(sdp))
  87. inode->i_fop = &gfs2_dir_fops_nolock;
  88. else
  89. inode->i_fop = &gfs2_dir_fops;
  90. } else if (S_ISLNK(mode)) {
  91. inode->i_op = &gfs2_symlink_iops;
  92. } else {
  93. inode->i_op = &gfs2_file_iops;
  94. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  95. }
  96. unlock_new_inode(inode);
  97. }
  98. /**
  99. * gfs2_inode_lookup - Lookup an inode
  100. * @sb: The super block
  101. * @no_addr: The inode number
  102. * @type: The type of the inode
  103. *
  104. * Returns: A VFS inode, or an error
  105. */
  106. struct inode *gfs2_inode_lookup(struct super_block *sb,
  107. unsigned int type,
  108. u64 no_addr,
  109. u64 no_formal_ino)
  110. {
  111. struct inode *inode;
  112. struct gfs2_inode *ip;
  113. struct gfs2_glock *io_gl = NULL;
  114. int error;
  115. inode = gfs2_iget(sb, no_addr);
  116. ip = GFS2_I(inode);
  117. if (!inode)
  118. return ERR_PTR(-ENOBUFS);
  119. if (inode->i_state & I_NEW) {
  120. struct gfs2_sbd *sdp = GFS2_SB(inode);
  121. ip->i_no_formal_ino = no_formal_ino;
  122. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  123. if (unlikely(error))
  124. goto fail;
  125. ip->i_gl->gl_object = ip;
  126. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  127. if (unlikely(error))
  128. goto fail_put;
  129. set_bit(GIF_INVALID, &ip->i_flags);
  130. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  131. if (unlikely(error))
  132. goto fail_iopen;
  133. ip->i_iopen_gh.gh_gl->gl_object = ip;
  134. gfs2_glock_put(io_gl);
  135. io_gl = NULL;
  136. if ((type == DT_UNKNOWN) && (no_formal_ino == 0))
  137. goto gfs2_nfsbypass;
  138. inode->i_mode = DT2IF(type);
  139. /*
  140. * We must read the inode in order to work out its type in
  141. * this case. Note that this doesn't happen often as we normally
  142. * know the type beforehand. This code path only occurs during
  143. * unlinked inode recovery (where it is safe to do this glock,
  144. * which is not true in the general case).
  145. */
  146. if (type == DT_UNKNOWN) {
  147. struct gfs2_holder gh;
  148. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  149. if (unlikely(error))
  150. goto fail_glock;
  151. /* Inode is now uptodate */
  152. gfs2_glock_dq_uninit(&gh);
  153. }
  154. gfs2_set_iop(inode);
  155. }
  156. gfs2_nfsbypass:
  157. return inode;
  158. fail_glock:
  159. gfs2_glock_dq(&ip->i_iopen_gh);
  160. fail_iopen:
  161. if (io_gl)
  162. gfs2_glock_put(io_gl);
  163. fail_put:
  164. if (inode->i_state & I_NEW)
  165. ip->i_gl->gl_object = NULL;
  166. gfs2_glock_put(ip->i_gl);
  167. fail:
  168. if (inode->i_state & I_NEW)
  169. iget_failed(inode);
  170. else
  171. iput(inode);
  172. return ERR_PTR(error);
  173. }
  174. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  175. u64 *no_formal_ino, unsigned int blktype)
  176. {
  177. struct super_block *sb = sdp->sd_vfs;
  178. struct gfs2_holder i_gh;
  179. struct inode *inode;
  180. int error;
  181. error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
  182. LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  183. if (error)
  184. return ERR_PTR(error);
  185. error = gfs2_check_blk_type(sdp, no_addr, blktype);
  186. if (error)
  187. goto fail;
  188. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0);
  189. if (IS_ERR(inode))
  190. goto fail;
  191. error = gfs2_inode_refresh(GFS2_I(inode));
  192. if (error)
  193. goto fail_iput;
  194. /* Pick up the works we bypass in gfs2_inode_lookup */
  195. if (inode->i_state & I_NEW)
  196. gfs2_set_iop(inode);
  197. /* Two extra checks for NFS only */
  198. if (no_formal_ino) {
  199. error = -ESTALE;
  200. if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
  201. goto fail_iput;
  202. error = -EIO;
  203. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
  204. goto fail_iput;
  205. error = 0;
  206. }
  207. fail:
  208. gfs2_glock_dq_uninit(&i_gh);
  209. return error ? ERR_PTR(error) : inode;
  210. fail_iput:
  211. iput(inode);
  212. goto fail;
  213. }
  214. static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
  215. {
  216. const struct gfs2_dinode *str = buf;
  217. struct timespec atime;
  218. u16 height, depth;
  219. if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
  220. goto corrupt;
  221. ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
  222. ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
  223. ip->i_inode.i_rdev = 0;
  224. switch (ip->i_inode.i_mode & S_IFMT) {
  225. case S_IFBLK:
  226. case S_IFCHR:
  227. ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
  228. be32_to_cpu(str->di_minor));
  229. break;
  230. };
  231. ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
  232. ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
  233. /*
  234. * We will need to review setting the nlink count here in the
  235. * light of the forthcoming ro bind mount work. This is a reminder
  236. * to do that.
  237. */
  238. ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
  239. i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
  240. gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
  241. atime.tv_sec = be64_to_cpu(str->di_atime);
  242. atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
  243. if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
  244. ip->i_inode.i_atime = atime;
  245. ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
  246. ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
  247. ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
  248. ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
  249. ip->i_goal = be64_to_cpu(str->di_goal_meta);
  250. ip->i_generation = be64_to_cpu(str->di_generation);
  251. ip->i_diskflags = be32_to_cpu(str->di_flags);
  252. gfs2_set_inode_flags(&ip->i_inode);
  253. height = be16_to_cpu(str->di_height);
  254. if (unlikely(height > GFS2_MAX_META_HEIGHT))
  255. goto corrupt;
  256. ip->i_height = (u8)height;
  257. depth = be16_to_cpu(str->di_depth);
  258. if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
  259. goto corrupt;
  260. ip->i_depth = (u8)depth;
  261. ip->i_entries = be32_to_cpu(str->di_entries);
  262. ip->i_eattr = be64_to_cpu(str->di_eattr);
  263. if (S_ISREG(ip->i_inode.i_mode))
  264. gfs2_set_aops(&ip->i_inode);
  265. return 0;
  266. corrupt:
  267. if (gfs2_consist_inode(ip))
  268. gfs2_dinode_print(ip);
  269. return -EIO;
  270. }
  271. /**
  272. * gfs2_inode_refresh - Refresh the incore copy of the dinode
  273. * @ip: The GFS2 inode
  274. *
  275. * Returns: errno
  276. */
  277. int gfs2_inode_refresh(struct gfs2_inode *ip)
  278. {
  279. struct buffer_head *dibh;
  280. int error;
  281. error = gfs2_meta_inode_buffer(ip, &dibh);
  282. if (error)
  283. return error;
  284. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
  285. brelse(dibh);
  286. return -EIO;
  287. }
  288. error = gfs2_dinode_in(ip, dibh->b_data);
  289. brelse(dibh);
  290. clear_bit(GIF_INVALID, &ip->i_flags);
  291. return error;
  292. }
  293. int gfs2_dinode_dealloc(struct gfs2_inode *ip)
  294. {
  295. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  296. struct gfs2_alloc *al;
  297. struct gfs2_rgrpd *rgd;
  298. int error;
  299. if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
  300. if (gfs2_consist_inode(ip))
  301. gfs2_dinode_print(ip);
  302. return -EIO;
  303. }
  304. al = gfs2_alloc_get(ip);
  305. if (!al)
  306. return -ENOMEM;
  307. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  308. if (error)
  309. goto out;
  310. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  311. if (error)
  312. goto out_qs;
  313. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  314. if (!rgd) {
  315. gfs2_consist_inode(ip);
  316. error = -EIO;
  317. goto out_rindex_relse;
  318. }
  319. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
  320. &al->al_rgd_gh);
  321. if (error)
  322. goto out_rindex_relse;
  323. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
  324. if (error)
  325. goto out_rg_gunlock;
  326. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  327. set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
  328. gfs2_free_di(rgd, ip);
  329. gfs2_trans_end(sdp);
  330. out_rg_gunlock:
  331. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  332. out_rindex_relse:
  333. gfs2_glock_dq_uninit(&al->al_ri_gh);
  334. out_qs:
  335. gfs2_quota_unhold(ip);
  336. out:
  337. gfs2_alloc_put(ip);
  338. return error;
  339. }
  340. /**
  341. * gfs2_change_nlink - Change nlink count on inode
  342. * @ip: The GFS2 inode
  343. * @diff: The change in the nlink count required
  344. *
  345. * Returns: errno
  346. */
  347. int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
  348. {
  349. struct buffer_head *dibh;
  350. u32 nlink;
  351. int error;
  352. BUG_ON(diff != 1 && diff != -1);
  353. nlink = ip->i_inode.i_nlink + diff;
  354. /* If we are reducing the nlink count, but the new value ends up being
  355. bigger than the old one, we must have underflowed. */
  356. if (diff < 0 && nlink > ip->i_inode.i_nlink) {
  357. if (gfs2_consist_inode(ip))
  358. gfs2_dinode_print(ip);
  359. return -EIO;
  360. }
  361. error = gfs2_meta_inode_buffer(ip, &dibh);
  362. if (error)
  363. return error;
  364. if (diff > 0)
  365. inc_nlink(&ip->i_inode);
  366. else
  367. drop_nlink(&ip->i_inode);
  368. ip->i_inode.i_ctime = CURRENT_TIME;
  369. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  370. gfs2_dinode_out(ip, dibh->b_data);
  371. brelse(dibh);
  372. mark_inode_dirty(&ip->i_inode);
  373. if (ip->i_inode.i_nlink == 0)
  374. gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
  375. return error;
  376. }
  377. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  378. {
  379. struct qstr qstr;
  380. struct inode *inode;
  381. gfs2_str2qstr(&qstr, name);
  382. inode = gfs2_lookupi(dip, &qstr, 1);
  383. /* gfs2_lookupi has inconsistent callers: vfs
  384. * related routines expect NULL for no entry found,
  385. * gfs2_lookup_simple callers expect ENOENT
  386. * and do not check for NULL.
  387. */
  388. if (inode == NULL)
  389. return ERR_PTR(-ENOENT);
  390. else
  391. return inode;
  392. }
  393. /**
  394. * gfs2_lookupi - Look up a filename in a directory and return its inode
  395. * @d_gh: An initialized holder for the directory glock
  396. * @name: The name of the inode to look for
  397. * @is_root: If 1, ignore the caller's permissions
  398. * @i_gh: An uninitialized holder for the new inode glock
  399. *
  400. * This can be called via the VFS filldir function when NFS is doing
  401. * a readdirplus and the inode which its intending to stat isn't
  402. * already in cache. In this case we must not take the directory glock
  403. * again, since the readdir call will have already taken that lock.
  404. *
  405. * Returns: errno
  406. */
  407. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  408. int is_root)
  409. {
  410. struct super_block *sb = dir->i_sb;
  411. struct gfs2_inode *dip = GFS2_I(dir);
  412. struct gfs2_holder d_gh;
  413. int error = 0;
  414. struct inode *inode = NULL;
  415. int unlock = 0;
  416. if (!name->len || name->len > GFS2_FNAMESIZE)
  417. return ERR_PTR(-ENAMETOOLONG);
  418. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  419. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  420. dir == sb->s_root->d_inode)) {
  421. igrab(dir);
  422. return dir;
  423. }
  424. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  425. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  426. if (error)
  427. return ERR_PTR(error);
  428. unlock = 1;
  429. }
  430. if (!is_root) {
  431. error = gfs2_permission(dir, MAY_EXEC, 0);
  432. if (error)
  433. goto out;
  434. }
  435. inode = gfs2_dir_search(dir, name);
  436. if (IS_ERR(inode))
  437. error = PTR_ERR(inode);
  438. out:
  439. if (unlock)
  440. gfs2_glock_dq_uninit(&d_gh);
  441. if (error == -ENOENT)
  442. return NULL;
  443. return inode ? inode : ERR_PTR(error);
  444. }
  445. /**
  446. * create_ok - OK to create a new on-disk inode here?
  447. * @dip: Directory in which dinode is to be created
  448. * @name: Name of new dinode
  449. * @mode:
  450. *
  451. * Returns: errno
  452. */
  453. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  454. unsigned int mode)
  455. {
  456. int error;
  457. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
  458. if (error)
  459. return error;
  460. /* Don't create entries in an unlinked directory */
  461. if (!dip->i_inode.i_nlink)
  462. return -EPERM;
  463. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  464. switch (error) {
  465. case -ENOENT:
  466. error = 0;
  467. break;
  468. case 0:
  469. return -EEXIST;
  470. default:
  471. return error;
  472. }
  473. if (dip->i_entries == (u32)-1)
  474. return -EFBIG;
  475. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  476. return -EMLINK;
  477. return 0;
  478. }
  479. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  480. unsigned int *uid, unsigned int *gid)
  481. {
  482. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  483. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  484. if (S_ISDIR(*mode))
  485. *mode |= S_ISUID;
  486. else if (dip->i_inode.i_uid != current_fsuid())
  487. *mode &= ~07111;
  488. *uid = dip->i_inode.i_uid;
  489. } else
  490. *uid = current_fsuid();
  491. if (dip->i_inode.i_mode & S_ISGID) {
  492. if (S_ISDIR(*mode))
  493. *mode |= S_ISGID;
  494. *gid = dip->i_inode.i_gid;
  495. } else
  496. *gid = current_fsgid();
  497. }
  498. static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
  499. {
  500. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  501. int error;
  502. if (gfs2_alloc_get(dip) == NULL)
  503. return -ENOMEM;
  504. dip->i_alloc->al_requested = RES_DINODE;
  505. error = gfs2_inplace_reserve(dip);
  506. if (error)
  507. goto out;
  508. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  509. if (error)
  510. goto out_ipreserv;
  511. error = gfs2_alloc_di(dip, no_addr, generation);
  512. gfs2_trans_end(sdp);
  513. out_ipreserv:
  514. gfs2_inplace_release(dip);
  515. out:
  516. gfs2_alloc_put(dip);
  517. return error;
  518. }
  519. /**
  520. * init_dinode - Fill in a new dinode structure
  521. * @dip: the directory this inode is being created in
  522. * @gl: The glock covering the new inode
  523. * @inum: the inode number
  524. * @mode: the file permissions
  525. * @uid:
  526. * @gid:
  527. *
  528. */
  529. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  530. const struct gfs2_inum_host *inum, unsigned int mode,
  531. unsigned int uid, unsigned int gid,
  532. const u64 *generation, dev_t dev, struct buffer_head **bhp)
  533. {
  534. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  535. struct gfs2_dinode *di;
  536. struct buffer_head *dibh;
  537. struct timespec tv = CURRENT_TIME;
  538. dibh = gfs2_meta_new(gl, inum->no_addr);
  539. gfs2_trans_add_bh(gl, dibh, 1);
  540. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  541. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  542. di = (struct gfs2_dinode *)dibh->b_data;
  543. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  544. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  545. di->di_mode = cpu_to_be32(mode);
  546. di->di_uid = cpu_to_be32(uid);
  547. di->di_gid = cpu_to_be32(gid);
  548. di->di_nlink = 0;
  549. di->di_size = 0;
  550. di->di_blocks = cpu_to_be64(1);
  551. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  552. di->di_major = cpu_to_be32(MAJOR(dev));
  553. di->di_minor = cpu_to_be32(MINOR(dev));
  554. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  555. di->di_generation = cpu_to_be64(*generation);
  556. di->di_flags = 0;
  557. if (S_ISREG(mode)) {
  558. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  559. gfs2_tune_get(sdp, gt_new_files_jdata))
  560. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  561. } else if (S_ISDIR(mode)) {
  562. di->di_flags |= cpu_to_be32(dip->i_diskflags &
  563. GFS2_DIF_INHERIT_JDATA);
  564. }
  565. di->__pad1 = 0;
  566. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  567. di->di_height = 0;
  568. di->__pad2 = 0;
  569. di->__pad3 = 0;
  570. di->di_depth = 0;
  571. di->di_entries = 0;
  572. memset(&di->__pad4, 0, sizeof(di->__pad4));
  573. di->di_eattr = 0;
  574. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  575. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  576. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  577. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  578. set_buffer_uptodate(dibh);
  579. *bhp = dibh;
  580. }
  581. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  582. unsigned int mode, const struct gfs2_inum_host *inum,
  583. const u64 *generation, dev_t dev, struct buffer_head **bhp)
  584. {
  585. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  586. unsigned int uid, gid;
  587. int error;
  588. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  589. if (!gfs2_alloc_get(dip))
  590. return -ENOMEM;
  591. error = gfs2_quota_lock(dip, uid, gid);
  592. if (error)
  593. goto out;
  594. error = gfs2_quota_check(dip, uid, gid);
  595. if (error)
  596. goto out_quota;
  597. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  598. if (error)
  599. goto out_quota;
  600. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
  601. gfs2_quota_change(dip, +1, uid, gid);
  602. gfs2_trans_end(sdp);
  603. out_quota:
  604. gfs2_quota_unlock(dip);
  605. out:
  606. gfs2_alloc_put(dip);
  607. return error;
  608. }
  609. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  610. struct gfs2_inode *ip)
  611. {
  612. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  613. struct gfs2_alloc *al;
  614. int alloc_required;
  615. struct buffer_head *dibh;
  616. int error;
  617. al = gfs2_alloc_get(dip);
  618. if (!al)
  619. return -ENOMEM;
  620. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  621. if (error)
  622. goto fail;
  623. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  624. if (alloc_required < 0)
  625. goto fail_quota_locks;
  626. if (alloc_required) {
  627. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  628. if (error)
  629. goto fail_quota_locks;
  630. al->al_requested = sdp->sd_max_dirres;
  631. error = gfs2_inplace_reserve(dip);
  632. if (error)
  633. goto fail_quota_locks;
  634. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  635. al->al_rgd->rd_length +
  636. 2 * RES_DINODE +
  637. RES_STATFS + RES_QUOTA, 0);
  638. if (error)
  639. goto fail_ipreserv;
  640. } else {
  641. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  642. if (error)
  643. goto fail_quota_locks;
  644. }
  645. error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
  646. if (error)
  647. goto fail_end_trans;
  648. error = gfs2_meta_inode_buffer(ip, &dibh);
  649. if (error)
  650. goto fail_end_trans;
  651. ip->i_inode.i_nlink = 1;
  652. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  653. gfs2_dinode_out(ip, dibh->b_data);
  654. brelse(dibh);
  655. return 0;
  656. fail_end_trans:
  657. gfs2_trans_end(sdp);
  658. fail_ipreserv:
  659. if (dip->i_alloc->al_rgd)
  660. gfs2_inplace_release(dip);
  661. fail_quota_locks:
  662. gfs2_quota_unlock(dip);
  663. fail:
  664. gfs2_alloc_put(dip);
  665. return error;
  666. }
  667. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
  668. {
  669. int err;
  670. size_t len;
  671. void *value;
  672. char *name;
  673. err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
  674. &name, &value, &len);
  675. if (err) {
  676. if (err == -EOPNOTSUPP)
  677. return 0;
  678. return err;
  679. }
  680. err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
  681. GFS2_EATYPE_SECURITY);
  682. kfree(value);
  683. kfree(name);
  684. return err;
  685. }
  686. /**
  687. * gfs2_createi - Create a new inode
  688. * @ghs: An array of two holders
  689. * @name: The name of the new file
  690. * @mode: the permissions on the new inode
  691. *
  692. * @ghs[0] is an initialized holder for the directory
  693. * @ghs[1] is the holder for the inode lock
  694. *
  695. * If the return value is not NULL, the glocks on both the directory and the new
  696. * file are held. A transaction has been started and an inplace reservation
  697. * is held, as well.
  698. *
  699. * Returns: An inode
  700. */
  701. struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
  702. unsigned int mode, dev_t dev)
  703. {
  704. struct inode *inode = NULL;
  705. struct gfs2_inode *dip = ghs->gh_gl->gl_object;
  706. struct inode *dir = &dip->i_inode;
  707. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  708. struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
  709. int error;
  710. u64 generation;
  711. struct buffer_head *bh = NULL;
  712. if (!name->len || name->len > GFS2_FNAMESIZE)
  713. return ERR_PTR(-ENAMETOOLONG);
  714. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  715. error = gfs2_glock_nq(ghs);
  716. if (error)
  717. goto fail;
  718. error = create_ok(dip, name, mode);
  719. if (error)
  720. goto fail_gunlock;
  721. error = alloc_dinode(dip, &inum.no_addr, &generation);
  722. if (error)
  723. goto fail_gunlock;
  724. inum.no_formal_ino = generation;
  725. error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
  726. LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  727. if (error)
  728. goto fail_gunlock;
  729. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
  730. if (error)
  731. goto fail_gunlock2;
  732. inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
  733. inum.no_formal_ino);
  734. if (IS_ERR(inode))
  735. goto fail_gunlock2;
  736. error = gfs2_inode_refresh(GFS2_I(inode));
  737. if (error)
  738. goto fail_gunlock2;
  739. error = gfs2_acl_create(dip, inode);
  740. if (error)
  741. goto fail_gunlock2;
  742. error = gfs2_security_init(dip, GFS2_I(inode));
  743. if (error)
  744. goto fail_gunlock2;
  745. error = link_dinode(dip, name, GFS2_I(inode));
  746. if (error)
  747. goto fail_gunlock2;
  748. if (bh)
  749. brelse(bh);
  750. return inode;
  751. fail_gunlock2:
  752. gfs2_glock_dq_uninit(ghs + 1);
  753. if (inode && !IS_ERR(inode))
  754. iput(inode);
  755. fail_gunlock:
  756. gfs2_glock_dq(ghs);
  757. fail:
  758. if (bh)
  759. brelse(bh);
  760. return ERR_PTR(error);
  761. }
  762. static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  763. {
  764. struct inode *inode = &ip->i_inode;
  765. struct buffer_head *dibh;
  766. int error;
  767. error = gfs2_meta_inode_buffer(ip, &dibh);
  768. if (error)
  769. return error;
  770. setattr_copy(inode, attr);
  771. mark_inode_dirty(inode);
  772. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  773. gfs2_dinode_out(ip, dibh->b_data);
  774. brelse(dibh);
  775. return 0;
  776. }
  777. /**
  778. * gfs2_setattr_simple -
  779. * @ip:
  780. * @attr:
  781. *
  782. * Called with a reference on the vnode.
  783. *
  784. * Returns: errno
  785. */
  786. int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  787. {
  788. int error;
  789. if (current->journal_info)
  790. return __gfs2_setattr_simple(ip, attr);
  791. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
  792. if (error)
  793. return error;
  794. error = __gfs2_setattr_simple(ip, attr);
  795. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  796. return error;
  797. }
  798. void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
  799. {
  800. struct gfs2_dinode *str = buf;
  801. str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  802. str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
  803. str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
  804. str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
  805. str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
  806. str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
  807. str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
  808. str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
  809. str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
  810. str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
  811. str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
  812. str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
  813. str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
  814. str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
  815. str->di_goal_meta = cpu_to_be64(ip->i_goal);
  816. str->di_goal_data = cpu_to_be64(ip->i_goal);
  817. str->di_generation = cpu_to_be64(ip->i_generation);
  818. str->di_flags = cpu_to_be32(ip->i_diskflags);
  819. str->di_height = cpu_to_be16(ip->i_height);
  820. str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
  821. !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
  822. GFS2_FORMAT_DE : 0);
  823. str->di_depth = cpu_to_be16(ip->i_depth);
  824. str->di_entries = cpu_to_be32(ip->i_entries);
  825. str->di_eattr = cpu_to_be64(ip->i_eattr);
  826. str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
  827. str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
  828. str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
  829. }
  830. void gfs2_dinode_print(const struct gfs2_inode *ip)
  831. {
  832. printk(KERN_INFO " no_formal_ino = %llu\n",
  833. (unsigned long long)ip->i_no_formal_ino);
  834. printk(KERN_INFO " no_addr = %llu\n",
  835. (unsigned long long)ip->i_no_addr);
  836. printk(KERN_INFO " i_size = %llu\n",
  837. (unsigned long long)i_size_read(&ip->i_inode));
  838. printk(KERN_INFO " blocks = %llu\n",
  839. (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
  840. printk(KERN_INFO " i_goal = %llu\n",
  841. (unsigned long long)ip->i_goal);
  842. printk(KERN_INFO " i_diskflags = 0x%.8X\n", ip->i_diskflags);
  843. printk(KERN_INFO " i_height = %u\n", ip->i_height);
  844. printk(KERN_INFO " i_depth = %u\n", ip->i_depth);
  845. printk(KERN_INFO " i_entries = %u\n", ip->i_entries);
  846. printk(KERN_INFO " i_eattr = %llu\n",
  847. (unsigned long long)ip->i_eattr);
  848. }