inode.c 24 KB

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