inode.c 25 KB

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