inode.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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 "eattr.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. static void gfs2_inum_range_in(struct gfs2_inum_range_host *ir, const void *buf)
  443. {
  444. const struct gfs2_inum_range *str = buf;
  445. ir->ir_start = be64_to_cpu(str->ir_start);
  446. ir->ir_length = be64_to_cpu(str->ir_length);
  447. }
  448. static void gfs2_inum_range_out(const struct gfs2_inum_range_host *ir, void *buf)
  449. {
  450. struct gfs2_inum_range *str = buf;
  451. str->ir_start = cpu_to_be64(ir->ir_start);
  452. str->ir_length = cpu_to_be64(ir->ir_length);
  453. }
  454. static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
  455. {
  456. struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
  457. struct buffer_head *bh;
  458. struct gfs2_inum_range_host ir;
  459. int error;
  460. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  461. if (error)
  462. return error;
  463. mutex_lock(&sdp->sd_inum_mutex);
  464. error = gfs2_meta_inode_buffer(ip, &bh);
  465. if (error) {
  466. mutex_unlock(&sdp->sd_inum_mutex);
  467. gfs2_trans_end(sdp);
  468. return error;
  469. }
  470. gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  471. if (ir.ir_length) {
  472. *formal_ino = ir.ir_start++;
  473. ir.ir_length--;
  474. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  475. gfs2_inum_range_out(&ir,
  476. bh->b_data + sizeof(struct gfs2_dinode));
  477. brelse(bh);
  478. mutex_unlock(&sdp->sd_inum_mutex);
  479. gfs2_trans_end(sdp);
  480. return 0;
  481. }
  482. brelse(bh);
  483. mutex_unlock(&sdp->sd_inum_mutex);
  484. gfs2_trans_end(sdp);
  485. return 1;
  486. }
  487. static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
  488. {
  489. struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
  490. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
  491. struct gfs2_holder gh;
  492. struct buffer_head *bh;
  493. struct gfs2_inum_range_host ir;
  494. int error;
  495. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  496. if (error)
  497. return error;
  498. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  499. if (error)
  500. goto out;
  501. mutex_lock(&sdp->sd_inum_mutex);
  502. error = gfs2_meta_inode_buffer(ip, &bh);
  503. if (error)
  504. goto out_end_trans;
  505. gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  506. if (!ir.ir_length) {
  507. struct buffer_head *m_bh;
  508. u64 x, y;
  509. __be64 z;
  510. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  511. if (error)
  512. goto out_brelse;
  513. z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
  514. x = y = be64_to_cpu(z);
  515. ir.ir_start = x;
  516. ir.ir_length = GFS2_INUM_QUANTUM;
  517. x += GFS2_INUM_QUANTUM;
  518. if (x < y)
  519. gfs2_consist_inode(m_ip);
  520. z = cpu_to_be64(x);
  521. gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
  522. *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
  523. brelse(m_bh);
  524. }
  525. *formal_ino = ir.ir_start++;
  526. ir.ir_length--;
  527. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  528. gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  529. out_brelse:
  530. brelse(bh);
  531. out_end_trans:
  532. mutex_unlock(&sdp->sd_inum_mutex);
  533. gfs2_trans_end(sdp);
  534. out:
  535. gfs2_glock_dq_uninit(&gh);
  536. return error;
  537. }
  538. static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
  539. {
  540. int error;
  541. error = pick_formal_ino_1(sdp, inum);
  542. if (error <= 0)
  543. return error;
  544. error = pick_formal_ino_2(sdp, inum);
  545. return error;
  546. }
  547. /**
  548. * create_ok - OK to create a new on-disk inode here?
  549. * @dip: Directory in which dinode is to be created
  550. * @name: Name of new dinode
  551. * @mode:
  552. *
  553. * Returns: errno
  554. */
  555. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  556. unsigned int mode)
  557. {
  558. int error;
  559. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  560. if (error)
  561. return error;
  562. /* Don't create entries in an unlinked directory */
  563. if (!dip->i_inode.i_nlink)
  564. return -EPERM;
  565. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  566. switch (error) {
  567. case -ENOENT:
  568. error = 0;
  569. break;
  570. case 0:
  571. return -EEXIST;
  572. default:
  573. return error;
  574. }
  575. if (dip->i_entries == (u32)-1)
  576. return -EFBIG;
  577. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  578. return -EMLINK;
  579. return 0;
  580. }
  581. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  582. unsigned int *uid, unsigned int *gid)
  583. {
  584. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  585. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  586. if (S_ISDIR(*mode))
  587. *mode |= S_ISUID;
  588. else if (dip->i_inode.i_uid != current_fsuid())
  589. *mode &= ~07111;
  590. *uid = dip->i_inode.i_uid;
  591. } else
  592. *uid = current_fsuid();
  593. if (dip->i_inode.i_mode & S_ISGID) {
  594. if (S_ISDIR(*mode))
  595. *mode |= S_ISGID;
  596. *gid = dip->i_inode.i_gid;
  597. } else
  598. *gid = current_fsgid();
  599. }
  600. static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
  601. {
  602. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  603. int error;
  604. if (gfs2_alloc_get(dip) == NULL)
  605. return -ENOMEM;
  606. dip->i_alloc->al_requested = RES_DINODE;
  607. error = gfs2_inplace_reserve(dip);
  608. if (error)
  609. goto out;
  610. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  611. if (error)
  612. goto out_ipreserv;
  613. *no_addr = gfs2_alloc_di(dip, generation);
  614. gfs2_trans_end(sdp);
  615. out_ipreserv:
  616. gfs2_inplace_release(dip);
  617. out:
  618. gfs2_alloc_put(dip);
  619. return error;
  620. }
  621. /**
  622. * init_dinode - Fill in a new dinode structure
  623. * @dip: the directory this inode is being created in
  624. * @gl: The glock covering the new inode
  625. * @inum: the inode number
  626. * @mode: the file permissions
  627. * @uid:
  628. * @gid:
  629. *
  630. */
  631. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  632. const struct gfs2_inum_host *inum, unsigned int mode,
  633. unsigned int uid, unsigned int gid,
  634. const u64 *generation, dev_t dev, struct buffer_head **bhp)
  635. {
  636. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  637. struct gfs2_dinode *di;
  638. struct buffer_head *dibh;
  639. struct timespec tv = CURRENT_TIME;
  640. dibh = gfs2_meta_new(gl, inum->no_addr);
  641. gfs2_trans_add_bh(gl, dibh, 1);
  642. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  643. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  644. di = (struct gfs2_dinode *)dibh->b_data;
  645. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  646. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  647. di->di_mode = cpu_to_be32(mode);
  648. di->di_uid = cpu_to_be32(uid);
  649. di->di_gid = cpu_to_be32(gid);
  650. di->di_nlink = 0;
  651. di->di_size = 0;
  652. di->di_blocks = cpu_to_be64(1);
  653. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  654. di->di_major = cpu_to_be32(MAJOR(dev));
  655. di->di_minor = cpu_to_be32(MINOR(dev));
  656. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  657. di->di_generation = cpu_to_be64(*generation);
  658. di->di_flags = 0;
  659. if (S_ISREG(mode)) {
  660. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  661. gfs2_tune_get(sdp, gt_new_files_jdata))
  662. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  663. } else if (S_ISDIR(mode)) {
  664. di->di_flags |= cpu_to_be32(dip->i_diskflags &
  665. GFS2_DIF_INHERIT_JDATA);
  666. }
  667. di->__pad1 = 0;
  668. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  669. di->di_height = 0;
  670. di->__pad2 = 0;
  671. di->__pad3 = 0;
  672. di->di_depth = 0;
  673. di->di_entries = 0;
  674. memset(&di->__pad4, 0, sizeof(di->__pad4));
  675. di->di_eattr = 0;
  676. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  677. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  678. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  679. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  680. set_buffer_uptodate(dibh);
  681. *bhp = dibh;
  682. }
  683. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  684. unsigned int mode, const struct gfs2_inum_host *inum,
  685. const u64 *generation, dev_t dev, struct buffer_head **bhp)
  686. {
  687. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  688. unsigned int uid, gid;
  689. int error;
  690. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  691. if (!gfs2_alloc_get(dip))
  692. return -ENOMEM;
  693. error = gfs2_quota_lock(dip, uid, gid);
  694. if (error)
  695. goto out;
  696. error = gfs2_quota_check(dip, uid, gid);
  697. if (error)
  698. goto out_quota;
  699. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  700. if (error)
  701. goto out_quota;
  702. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
  703. gfs2_quota_change(dip, +1, uid, gid);
  704. gfs2_trans_end(sdp);
  705. out_quota:
  706. gfs2_quota_unlock(dip);
  707. out:
  708. gfs2_alloc_put(dip);
  709. return error;
  710. }
  711. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  712. struct gfs2_inode *ip)
  713. {
  714. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  715. struct gfs2_alloc *al;
  716. int alloc_required;
  717. struct buffer_head *dibh;
  718. int error;
  719. al = gfs2_alloc_get(dip);
  720. if (!al)
  721. return -ENOMEM;
  722. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  723. if (error)
  724. goto fail;
  725. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  726. if (alloc_required < 0)
  727. goto fail_quota_locks;
  728. if (alloc_required) {
  729. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  730. if (error)
  731. goto fail_quota_locks;
  732. al->al_requested = sdp->sd_max_dirres;
  733. error = gfs2_inplace_reserve(dip);
  734. if (error)
  735. goto fail_quota_locks;
  736. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  737. al->al_rgd->rd_length +
  738. 2 * RES_DINODE +
  739. RES_STATFS + RES_QUOTA, 0);
  740. if (error)
  741. goto fail_ipreserv;
  742. } else {
  743. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  744. if (error)
  745. goto fail_quota_locks;
  746. }
  747. error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
  748. if (error)
  749. goto fail_end_trans;
  750. error = gfs2_meta_inode_buffer(ip, &dibh);
  751. if (error)
  752. goto fail_end_trans;
  753. ip->i_inode.i_nlink = 1;
  754. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  755. gfs2_dinode_out(ip, dibh->b_data);
  756. brelse(dibh);
  757. return 0;
  758. fail_end_trans:
  759. gfs2_trans_end(sdp);
  760. fail_ipreserv:
  761. if (dip->i_alloc->al_rgd)
  762. gfs2_inplace_release(dip);
  763. fail_quota_locks:
  764. gfs2_quota_unlock(dip);
  765. fail:
  766. gfs2_alloc_put(dip);
  767. return error;
  768. }
  769. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
  770. {
  771. int err;
  772. size_t len;
  773. void *value;
  774. char *name;
  775. struct gfs2_ea_request er;
  776. err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
  777. &name, &value, &len);
  778. if (err) {
  779. if (err == -EOPNOTSUPP)
  780. return 0;
  781. return err;
  782. }
  783. memset(&er, 0, sizeof(struct gfs2_ea_request));
  784. er.er_type = GFS2_EATYPE_SECURITY;
  785. er.er_name = name;
  786. er.er_data = value;
  787. er.er_name_len = strlen(name);
  788. er.er_data_len = len;
  789. err = gfs2_ea_set_i(ip, &er);
  790. kfree(value);
  791. kfree(name);
  792. return err;
  793. }
  794. /**
  795. * gfs2_createi - Create a new inode
  796. * @ghs: An array of two holders
  797. * @name: The name of the new file
  798. * @mode: the permissions on the new inode
  799. *
  800. * @ghs[0] is an initialized holder for the directory
  801. * @ghs[1] is the holder for the inode lock
  802. *
  803. * If the return value is not NULL, the glocks on both the directory and the new
  804. * file are held. A transaction has been started and an inplace reservation
  805. * is held, as well.
  806. *
  807. * Returns: An inode
  808. */
  809. struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
  810. unsigned int mode, dev_t dev)
  811. {
  812. struct inode *inode = NULL;
  813. struct gfs2_inode *dip = ghs->gh_gl->gl_object;
  814. struct inode *dir = &dip->i_inode;
  815. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  816. struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
  817. int error;
  818. u64 generation;
  819. struct buffer_head *bh = NULL;
  820. if (!name->len || name->len > GFS2_FNAMESIZE)
  821. return ERR_PTR(-ENAMETOOLONG);
  822. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  823. error = gfs2_glock_nq(ghs);
  824. if (error)
  825. goto fail;
  826. error = create_ok(dip, name, mode);
  827. if (error)
  828. goto fail_gunlock;
  829. error = pick_formal_ino(sdp, &inum.no_formal_ino);
  830. if (error)
  831. goto fail_gunlock;
  832. error = alloc_dinode(dip, &inum.no_addr, &generation);
  833. if (error)
  834. goto fail_gunlock;
  835. error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
  836. LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  837. if (error)
  838. goto fail_gunlock;
  839. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
  840. if (error)
  841. goto fail_gunlock2;
  842. inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode),
  843. inum.no_addr,
  844. inum.no_formal_ino, 0);
  845. if (IS_ERR(inode))
  846. goto fail_gunlock2;
  847. error = gfs2_inode_refresh(GFS2_I(inode));
  848. if (error)
  849. goto fail_gunlock2;
  850. error = gfs2_acl_create(dip, GFS2_I(inode));
  851. if (error)
  852. goto fail_gunlock2;
  853. error = gfs2_security_init(dip, GFS2_I(inode));
  854. if (error)
  855. goto fail_gunlock2;
  856. error = link_dinode(dip, name, GFS2_I(inode));
  857. if (error)
  858. goto fail_gunlock2;
  859. if (bh)
  860. brelse(bh);
  861. return inode;
  862. fail_gunlock2:
  863. gfs2_glock_dq_uninit(ghs + 1);
  864. if (inode && !IS_ERR(inode))
  865. iput(inode);
  866. fail_gunlock:
  867. gfs2_glock_dq(ghs);
  868. fail:
  869. if (bh)
  870. brelse(bh);
  871. return ERR_PTR(error);
  872. }
  873. static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  874. {
  875. struct buffer_head *dibh;
  876. int error;
  877. error = gfs2_meta_inode_buffer(ip, &dibh);
  878. if (!error) {
  879. error = inode_setattr(&ip->i_inode, attr);
  880. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  881. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  882. gfs2_dinode_out(ip, dibh->b_data);
  883. brelse(dibh);
  884. }
  885. return error;
  886. }
  887. /**
  888. * gfs2_setattr_simple -
  889. * @ip:
  890. * @attr:
  891. *
  892. * Called with a reference on the vnode.
  893. *
  894. * Returns: errno
  895. */
  896. int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  897. {
  898. int error;
  899. if (current->journal_info)
  900. return __gfs2_setattr_simple(ip, attr);
  901. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
  902. if (error)
  903. return error;
  904. error = __gfs2_setattr_simple(ip, attr);
  905. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  906. return error;
  907. }
  908. void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
  909. {
  910. struct gfs2_dinode *str = buf;
  911. str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  912. str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
  913. str->di_header.__pad0 = 0;
  914. str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
  915. str->di_header.__pad1 = 0;
  916. str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
  917. str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
  918. str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
  919. str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
  920. str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
  921. str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
  922. str->di_size = cpu_to_be64(ip->i_disksize);
  923. str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
  924. str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
  925. str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
  926. str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
  927. str->di_goal_meta = cpu_to_be64(ip->i_goal);
  928. str->di_goal_data = cpu_to_be64(ip->i_goal);
  929. str->di_generation = cpu_to_be64(ip->i_generation);
  930. str->di_flags = cpu_to_be32(ip->i_diskflags);
  931. str->di_height = cpu_to_be16(ip->i_height);
  932. str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
  933. !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
  934. GFS2_FORMAT_DE : 0);
  935. str->di_depth = cpu_to_be16(ip->i_depth);
  936. str->di_entries = cpu_to_be32(ip->i_entries);
  937. str->di_eattr = cpu_to_be64(ip->i_eattr);
  938. str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
  939. str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
  940. str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
  941. }
  942. void gfs2_dinode_print(const struct gfs2_inode *ip)
  943. {
  944. printk(KERN_INFO " no_formal_ino = %llu\n",
  945. (unsigned long long)ip->i_no_formal_ino);
  946. printk(KERN_INFO " no_addr = %llu\n",
  947. (unsigned long long)ip->i_no_addr);
  948. printk(KERN_INFO " i_disksize = %llu\n",
  949. (unsigned long long)ip->i_disksize);
  950. printk(KERN_INFO " blocks = %llu\n",
  951. (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
  952. printk(KERN_INFO " i_goal = %llu\n",
  953. (unsigned long long)ip->i_goal);
  954. printk(KERN_INFO " i_diskflags = 0x%.8X\n", ip->i_diskflags);
  955. printk(KERN_INFO " i_height = %u\n", ip->i_height);
  956. printk(KERN_INFO " i_depth = %u\n", ip->i_depth);
  957. printk(KERN_INFO " i_entries = %u\n", ip->i_entries);
  958. printk(KERN_INFO " i_eattr = %llu\n",
  959. (unsigned long long)ip->i_eattr);
  960. }