inode.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 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/lm_interface.h>
  19. #include <linux/security.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 "ops_address.h"
  32. #include "ops_file.h"
  33. #include "ops_inode.h"
  34. #include "quota.h"
  35. #include "rgrp.h"
  36. #include "trans.h"
  37. #include "util.h"
  38. static int iget_test(struct inode *inode, void *opaque)
  39. {
  40. struct gfs2_inode *ip = GFS2_I(inode);
  41. u64 *no_addr = opaque;
  42. if (ip->i_no_addr == *no_addr &&
  43. inode->i_private != NULL)
  44. return 1;
  45. return 0;
  46. }
  47. static int iget_set(struct inode *inode, void *opaque)
  48. {
  49. struct gfs2_inode *ip = GFS2_I(inode);
  50. u64 *no_addr = opaque;
  51. inode->i_ino = (unsigned long)*no_addr;
  52. ip->i_no_addr = *no_addr;
  53. return 0;
  54. }
  55. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
  56. {
  57. unsigned long hash = (unsigned long)no_addr;
  58. return ilookup5(sb, hash, iget_test, &no_addr);
  59. }
  60. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
  61. {
  62. unsigned long hash = (unsigned long)no_addr;
  63. return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
  64. }
  65. /**
  66. * gfs2_inode_lookup - Lookup an inode
  67. * @sb: The super block
  68. * @no_addr: The inode number
  69. * @type: The type of the inode
  70. *
  71. * Returns: A VFS inode, or an error
  72. */
  73. struct inode *gfs2_inode_lookup(struct super_block *sb, u64 no_addr, unsigned int type)
  74. {
  75. struct inode *inode = gfs2_iget(sb, no_addr);
  76. struct gfs2_inode *ip = GFS2_I(inode);
  77. struct gfs2_glock *io_gl;
  78. int error;
  79. if (!inode)
  80. return ERR_PTR(-ENOBUFS);
  81. if (inode->i_state & I_NEW) {
  82. struct gfs2_sbd *sdp = GFS2_SB(inode);
  83. umode_t mode = DT2IF(type);
  84. inode->i_private = ip;
  85. inode->i_mode = mode;
  86. if (S_ISREG(mode)) {
  87. inode->i_op = &gfs2_file_iops;
  88. inode->i_fop = &gfs2_file_fops;
  89. inode->i_mapping->a_ops = &gfs2_file_aops;
  90. } else if (S_ISDIR(mode)) {
  91. inode->i_op = &gfs2_dir_iops;
  92. inode->i_fop = &gfs2_dir_fops;
  93. } else if (S_ISLNK(mode)) {
  94. inode->i_op = &gfs2_symlink_iops;
  95. } else {
  96. inode->i_op = &gfs2_dev_iops;
  97. }
  98. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  99. if (unlikely(error))
  100. goto fail;
  101. ip->i_gl->gl_object = ip;
  102. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  103. if (unlikely(error))
  104. goto fail_put;
  105. set_bit(GIF_INVALID, &ip->i_flags);
  106. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  107. if (unlikely(error))
  108. goto fail_iopen;
  109. gfs2_glock_put(io_gl);
  110. unlock_new_inode(inode);
  111. }
  112. return inode;
  113. fail_iopen:
  114. gfs2_glock_put(io_gl);
  115. fail_put:
  116. ip->i_gl->gl_object = NULL;
  117. gfs2_glock_put(ip->i_gl);
  118. fail:
  119. iput(inode);
  120. return ERR_PTR(error);
  121. }
  122. static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
  123. {
  124. struct gfs2_dinode_host *di = &ip->i_di;
  125. const struct gfs2_dinode *str = buf;
  126. if (ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)) {
  127. if (gfs2_consist_inode(ip))
  128. gfs2_dinode_print(ip);
  129. return -EIO;
  130. }
  131. ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
  132. ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
  133. ip->i_inode.i_rdev = 0;
  134. switch (ip->i_inode.i_mode & S_IFMT) {
  135. case S_IFBLK:
  136. case S_IFCHR:
  137. ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
  138. be32_to_cpu(str->di_minor));
  139. break;
  140. };
  141. ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
  142. ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
  143. /*
  144. * We will need to review setting the nlink count here in the
  145. * light of the forthcoming ro bind mount work. This is a reminder
  146. * to do that.
  147. */
  148. ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
  149. di->di_size = be64_to_cpu(str->di_size);
  150. i_size_write(&ip->i_inode, di->di_size);
  151. di->di_blocks = be64_to_cpu(str->di_blocks);
  152. gfs2_set_inode_blocks(&ip->i_inode);
  153. ip->i_inode.i_atime.tv_sec = be64_to_cpu(str->di_atime);
  154. ip->i_inode.i_atime.tv_nsec = 0;
  155. ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
  156. ip->i_inode.i_mtime.tv_nsec = 0;
  157. ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
  158. ip->i_inode.i_ctime.tv_nsec = 0;
  159. di->di_goal_meta = be64_to_cpu(str->di_goal_meta);
  160. di->di_goal_data = be64_to_cpu(str->di_goal_data);
  161. di->di_generation = be64_to_cpu(str->di_generation);
  162. di->di_flags = be32_to_cpu(str->di_flags);
  163. gfs2_set_inode_flags(&ip->i_inode);
  164. di->di_height = be16_to_cpu(str->di_height);
  165. di->di_depth = be16_to_cpu(str->di_depth);
  166. di->di_entries = be32_to_cpu(str->di_entries);
  167. di->di_eattr = be64_to_cpu(str->di_eattr);
  168. return 0;
  169. }
  170. /**
  171. * gfs2_inode_refresh - Refresh the incore copy of the dinode
  172. * @ip: The GFS2 inode
  173. *
  174. * Returns: errno
  175. */
  176. int gfs2_inode_refresh(struct gfs2_inode *ip)
  177. {
  178. struct buffer_head *dibh;
  179. int error;
  180. error = gfs2_meta_inode_buffer(ip, &dibh);
  181. if (error)
  182. return error;
  183. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
  184. brelse(dibh);
  185. return -EIO;
  186. }
  187. error = gfs2_dinode_in(ip, dibh->b_data);
  188. brelse(dibh);
  189. clear_bit(GIF_INVALID, &ip->i_flags);
  190. return error;
  191. }
  192. int gfs2_dinode_dealloc(struct gfs2_inode *ip)
  193. {
  194. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  195. struct gfs2_alloc *al;
  196. struct gfs2_rgrpd *rgd;
  197. int error;
  198. if (ip->i_di.di_blocks != 1) {
  199. if (gfs2_consist_inode(ip))
  200. gfs2_dinode_print(ip);
  201. return -EIO;
  202. }
  203. al = gfs2_alloc_get(ip);
  204. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  205. if (error)
  206. goto out;
  207. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  208. if (error)
  209. goto out_qs;
  210. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  211. if (!rgd) {
  212. gfs2_consist_inode(ip);
  213. error = -EIO;
  214. goto out_rindex_relse;
  215. }
  216. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
  217. &al->al_rgd_gh);
  218. if (error)
  219. goto out_rindex_relse;
  220. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
  221. if (error)
  222. goto out_rg_gunlock;
  223. gfs2_trans_add_gl(ip->i_gl);
  224. gfs2_free_di(rgd, ip);
  225. gfs2_trans_end(sdp);
  226. clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
  227. out_rg_gunlock:
  228. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  229. out_rindex_relse:
  230. gfs2_glock_dq_uninit(&al->al_ri_gh);
  231. out_qs:
  232. gfs2_quota_unhold(ip);
  233. out:
  234. gfs2_alloc_put(ip);
  235. return error;
  236. }
  237. /**
  238. * gfs2_change_nlink - Change nlink count on inode
  239. * @ip: The GFS2 inode
  240. * @diff: The change in the nlink count required
  241. *
  242. * Returns: errno
  243. */
  244. int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
  245. {
  246. struct buffer_head *dibh;
  247. u32 nlink;
  248. int error;
  249. BUG_ON(diff != 1 && diff != -1);
  250. nlink = ip->i_inode.i_nlink + diff;
  251. /* If we are reducing the nlink count, but the new value ends up being
  252. bigger than the old one, we must have underflowed. */
  253. if (diff < 0 && nlink > ip->i_inode.i_nlink) {
  254. if (gfs2_consist_inode(ip))
  255. gfs2_dinode_print(ip);
  256. return -EIO;
  257. }
  258. error = gfs2_meta_inode_buffer(ip, &dibh);
  259. if (error)
  260. return error;
  261. if (diff > 0)
  262. inc_nlink(&ip->i_inode);
  263. else
  264. drop_nlink(&ip->i_inode);
  265. ip->i_inode.i_ctime = CURRENT_TIME_SEC;
  266. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  267. gfs2_dinode_out(ip, dibh->b_data);
  268. brelse(dibh);
  269. mark_inode_dirty(&ip->i_inode);
  270. if (ip->i_inode.i_nlink == 0)
  271. gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
  272. return error;
  273. }
  274. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  275. {
  276. struct qstr qstr;
  277. struct inode *inode;
  278. gfs2_str2qstr(&qstr, name);
  279. inode = gfs2_lookupi(dip, &qstr, 1, NULL);
  280. /* gfs2_lookupi has inconsistent callers: vfs
  281. * related routines expect NULL for no entry found,
  282. * gfs2_lookup_simple callers expect ENOENT
  283. * and do not check for NULL.
  284. */
  285. if (inode == NULL)
  286. return ERR_PTR(-ENOENT);
  287. else
  288. return inode;
  289. }
  290. /**
  291. * gfs2_lookupi - Look up a filename in a directory and return its inode
  292. * @d_gh: An initialized holder for the directory glock
  293. * @name: The name of the inode to look for
  294. * @is_root: If 1, ignore the caller's permissions
  295. * @i_gh: An uninitialized holder for the new inode glock
  296. *
  297. * This can be called via the VFS filldir function when NFS is doing
  298. * a readdirplus and the inode which its intending to stat isn't
  299. * already in cache. In this case we must not take the directory glock
  300. * again, since the readdir call will have already taken that lock.
  301. *
  302. * Returns: errno
  303. */
  304. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  305. int is_root, struct nameidata *nd)
  306. {
  307. struct super_block *sb = dir->i_sb;
  308. struct gfs2_inode *dip = GFS2_I(dir);
  309. struct gfs2_holder d_gh;
  310. int error;
  311. struct inode *inode = NULL;
  312. int unlock = 0;
  313. if (!name->len || name->len > GFS2_FNAMESIZE)
  314. return ERR_PTR(-ENAMETOOLONG);
  315. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  316. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  317. dir == sb->s_root->d_inode)) {
  318. igrab(dir);
  319. return dir;
  320. }
  321. if (gfs2_glock_is_locked_by_me(dip->i_gl) == 0) {
  322. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  323. if (error)
  324. return ERR_PTR(error);
  325. unlock = 1;
  326. }
  327. if (!is_root) {
  328. error = permission(dir, MAY_EXEC, NULL);
  329. if (error)
  330. goto out;
  331. }
  332. inode = gfs2_dir_search(dir, name);
  333. if (IS_ERR(inode))
  334. error = PTR_ERR(inode);
  335. out:
  336. if (unlock)
  337. gfs2_glock_dq_uninit(&d_gh);
  338. if (error == -ENOENT)
  339. return NULL;
  340. return inode ? inode : ERR_PTR(error);
  341. }
  342. static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
  343. {
  344. struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
  345. struct buffer_head *bh;
  346. struct gfs2_inum_range_host ir;
  347. int error;
  348. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  349. if (error)
  350. return error;
  351. mutex_lock(&sdp->sd_inum_mutex);
  352. error = gfs2_meta_inode_buffer(ip, &bh);
  353. if (error) {
  354. mutex_unlock(&sdp->sd_inum_mutex);
  355. gfs2_trans_end(sdp);
  356. return error;
  357. }
  358. gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  359. if (ir.ir_length) {
  360. *formal_ino = ir.ir_start++;
  361. ir.ir_length--;
  362. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  363. gfs2_inum_range_out(&ir,
  364. bh->b_data + sizeof(struct gfs2_dinode));
  365. brelse(bh);
  366. mutex_unlock(&sdp->sd_inum_mutex);
  367. gfs2_trans_end(sdp);
  368. return 0;
  369. }
  370. brelse(bh);
  371. mutex_unlock(&sdp->sd_inum_mutex);
  372. gfs2_trans_end(sdp);
  373. return 1;
  374. }
  375. static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
  376. {
  377. struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
  378. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
  379. struct gfs2_holder gh;
  380. struct buffer_head *bh;
  381. struct gfs2_inum_range_host ir;
  382. int error;
  383. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  384. if (error)
  385. return error;
  386. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  387. if (error)
  388. goto out;
  389. mutex_lock(&sdp->sd_inum_mutex);
  390. error = gfs2_meta_inode_buffer(ip, &bh);
  391. if (error)
  392. goto out_end_trans;
  393. gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  394. if (!ir.ir_length) {
  395. struct buffer_head *m_bh;
  396. u64 x, y;
  397. __be64 z;
  398. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  399. if (error)
  400. goto out_brelse;
  401. z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
  402. x = y = be64_to_cpu(z);
  403. ir.ir_start = x;
  404. ir.ir_length = GFS2_INUM_QUANTUM;
  405. x += GFS2_INUM_QUANTUM;
  406. if (x < y)
  407. gfs2_consist_inode(m_ip);
  408. z = cpu_to_be64(x);
  409. gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
  410. *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
  411. brelse(m_bh);
  412. }
  413. *formal_ino = ir.ir_start++;
  414. ir.ir_length--;
  415. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  416. gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  417. out_brelse:
  418. brelse(bh);
  419. out_end_trans:
  420. mutex_unlock(&sdp->sd_inum_mutex);
  421. gfs2_trans_end(sdp);
  422. out:
  423. gfs2_glock_dq_uninit(&gh);
  424. return error;
  425. }
  426. static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
  427. {
  428. int error;
  429. error = pick_formal_ino_1(sdp, inum);
  430. if (error <= 0)
  431. return error;
  432. error = pick_formal_ino_2(sdp, inum);
  433. return error;
  434. }
  435. /**
  436. * create_ok - OK to create a new on-disk inode here?
  437. * @dip: Directory in which dinode is to be created
  438. * @name: Name of new dinode
  439. * @mode:
  440. *
  441. * Returns: errno
  442. */
  443. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  444. unsigned int mode)
  445. {
  446. int error;
  447. error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
  448. if (error)
  449. return error;
  450. /* Don't create entries in an unlinked directory */
  451. if (!dip->i_inode.i_nlink)
  452. return -EPERM;
  453. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  454. switch (error) {
  455. case -ENOENT:
  456. error = 0;
  457. break;
  458. case 0:
  459. return -EEXIST;
  460. default:
  461. return error;
  462. }
  463. if (dip->i_di.di_entries == (u32)-1)
  464. return -EFBIG;
  465. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  466. return -EMLINK;
  467. return 0;
  468. }
  469. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  470. unsigned int *uid, unsigned int *gid)
  471. {
  472. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  473. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  474. if (S_ISDIR(*mode))
  475. *mode |= S_ISUID;
  476. else if (dip->i_inode.i_uid != current->fsuid)
  477. *mode &= ~07111;
  478. *uid = dip->i_inode.i_uid;
  479. } else
  480. *uid = current->fsuid;
  481. if (dip->i_inode.i_mode & S_ISGID) {
  482. if (S_ISDIR(*mode))
  483. *mode |= S_ISGID;
  484. *gid = dip->i_inode.i_gid;
  485. } else
  486. *gid = current->fsgid;
  487. }
  488. static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
  489. {
  490. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  491. int error;
  492. gfs2_alloc_get(dip);
  493. dip->i_alloc.al_requested = RES_DINODE;
  494. error = gfs2_inplace_reserve(dip);
  495. if (error)
  496. goto out;
  497. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  498. if (error)
  499. goto out_ipreserv;
  500. *no_addr = gfs2_alloc_di(dip, generation);
  501. gfs2_trans_end(sdp);
  502. out_ipreserv:
  503. gfs2_inplace_release(dip);
  504. out:
  505. gfs2_alloc_put(dip);
  506. return error;
  507. }
  508. /**
  509. * init_dinode - Fill in a new dinode structure
  510. * @dip: the directory this inode is being created in
  511. * @gl: The glock covering the new inode
  512. * @inum: the inode number
  513. * @mode: the file permissions
  514. * @uid:
  515. * @gid:
  516. *
  517. */
  518. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  519. const struct gfs2_inum_host *inum, unsigned int mode,
  520. unsigned int uid, unsigned int gid,
  521. const u64 *generation, dev_t dev)
  522. {
  523. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  524. struct gfs2_dinode *di;
  525. struct buffer_head *dibh;
  526. dibh = gfs2_meta_new(gl, inum->no_addr);
  527. gfs2_trans_add_bh(gl, dibh, 1);
  528. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  529. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  530. di = (struct gfs2_dinode *)dibh->b_data;
  531. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  532. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  533. di->di_mode = cpu_to_be32(mode);
  534. di->di_uid = cpu_to_be32(uid);
  535. di->di_gid = cpu_to_be32(gid);
  536. di->di_nlink = 0;
  537. di->di_size = 0;
  538. di->di_blocks = cpu_to_be64(1);
  539. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
  540. di->di_major = cpu_to_be32(MAJOR(dev));
  541. di->di_minor = cpu_to_be32(MINOR(dev));
  542. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  543. di->di_generation = cpu_to_be64(*generation);
  544. di->di_flags = 0;
  545. if (S_ISREG(mode)) {
  546. if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
  547. gfs2_tune_get(sdp, gt_new_files_jdata))
  548. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  549. if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
  550. gfs2_tune_get(sdp, gt_new_files_directio))
  551. di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
  552. } else if (S_ISDIR(mode)) {
  553. di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
  554. GFS2_DIF_INHERIT_DIRECTIO);
  555. di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
  556. GFS2_DIF_INHERIT_JDATA);
  557. }
  558. di->__pad1 = 0;
  559. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  560. di->di_height = 0;
  561. di->__pad2 = 0;
  562. di->__pad3 = 0;
  563. di->di_depth = 0;
  564. di->di_entries = 0;
  565. memset(&di->__pad4, 0, sizeof(di->__pad4));
  566. di->di_eattr = 0;
  567. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  568. brelse(dibh);
  569. }
  570. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  571. unsigned int mode, const struct gfs2_inum_host *inum,
  572. const u64 *generation, dev_t dev)
  573. {
  574. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  575. unsigned int uid, gid;
  576. int error;
  577. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  578. gfs2_alloc_get(dip);
  579. error = gfs2_quota_lock(dip, uid, gid);
  580. if (error)
  581. goto out;
  582. error = gfs2_quota_check(dip, uid, gid);
  583. if (error)
  584. goto out_quota;
  585. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  586. if (error)
  587. goto out_quota;
  588. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev);
  589. gfs2_quota_change(dip, +1, uid, gid);
  590. gfs2_trans_end(sdp);
  591. out_quota:
  592. gfs2_quota_unlock(dip);
  593. out:
  594. gfs2_alloc_put(dip);
  595. return error;
  596. }
  597. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  598. struct gfs2_inode *ip)
  599. {
  600. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  601. struct gfs2_alloc *al;
  602. int alloc_required;
  603. struct buffer_head *dibh;
  604. int error;
  605. al = gfs2_alloc_get(dip);
  606. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  607. if (error)
  608. goto fail;
  609. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  610. if (alloc_required < 0)
  611. goto fail;
  612. if (alloc_required) {
  613. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  614. if (error)
  615. goto fail_quota_locks;
  616. al->al_requested = sdp->sd_max_dirres;
  617. error = gfs2_inplace_reserve(dip);
  618. if (error)
  619. goto fail_quota_locks;
  620. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  621. al->al_rgd->rd_ri.ri_length +
  622. 2 * RES_DINODE +
  623. RES_STATFS + RES_QUOTA, 0);
  624. if (error)
  625. goto fail_ipreserv;
  626. } else {
  627. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  628. if (error)
  629. goto fail_quota_locks;
  630. }
  631. error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
  632. if (error)
  633. goto fail_end_trans;
  634. error = gfs2_meta_inode_buffer(ip, &dibh);
  635. if (error)
  636. goto fail_end_trans;
  637. ip->i_inode.i_nlink = 1;
  638. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  639. gfs2_dinode_out(ip, dibh->b_data);
  640. brelse(dibh);
  641. return 0;
  642. fail_end_trans:
  643. gfs2_trans_end(sdp);
  644. fail_ipreserv:
  645. if (dip->i_alloc.al_rgd)
  646. gfs2_inplace_release(dip);
  647. fail_quota_locks:
  648. gfs2_quota_unlock(dip);
  649. fail:
  650. gfs2_alloc_put(dip);
  651. return error;
  652. }
  653. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
  654. {
  655. int err;
  656. size_t len;
  657. void *value;
  658. char *name;
  659. struct gfs2_ea_request er;
  660. err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
  661. &name, &value, &len);
  662. if (err) {
  663. if (err == -EOPNOTSUPP)
  664. return 0;
  665. return err;
  666. }
  667. memset(&er, 0, sizeof(struct gfs2_ea_request));
  668. er.er_type = GFS2_EATYPE_SECURITY;
  669. er.er_name = name;
  670. er.er_data = value;
  671. er.er_name_len = strlen(name);
  672. er.er_data_len = len;
  673. err = gfs2_ea_set_i(ip, &er);
  674. kfree(value);
  675. kfree(name);
  676. return err;
  677. }
  678. /**
  679. * gfs2_createi - Create a new inode
  680. * @ghs: An array of two holders
  681. * @name: The name of the new file
  682. * @mode: the permissions on the new inode
  683. *
  684. * @ghs[0] is an initialized holder for the directory
  685. * @ghs[1] is the holder for the inode lock
  686. *
  687. * If the return value is not NULL, the glocks on both the directory and the new
  688. * file are held. A transaction has been started and an inplace reservation
  689. * is held, as well.
  690. *
  691. * Returns: An inode
  692. */
  693. struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
  694. unsigned int mode, dev_t dev)
  695. {
  696. struct inode *inode;
  697. struct gfs2_inode *dip = ghs->gh_gl->gl_object;
  698. struct inode *dir = &dip->i_inode;
  699. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  700. struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
  701. int error;
  702. u64 generation;
  703. if (!name->len || name->len > GFS2_FNAMESIZE)
  704. return ERR_PTR(-ENAMETOOLONG);
  705. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  706. error = gfs2_glock_nq(ghs);
  707. if (error)
  708. goto fail;
  709. error = create_ok(dip, name, mode);
  710. if (error)
  711. goto fail_gunlock;
  712. error = pick_formal_ino(sdp, &inum.no_formal_ino);
  713. if (error)
  714. goto fail_gunlock;
  715. error = alloc_dinode(dip, &inum.no_addr, &generation);
  716. if (error)
  717. goto fail_gunlock;
  718. error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
  719. LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  720. if (error)
  721. goto fail_gunlock;
  722. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev);
  723. if (error)
  724. goto fail_gunlock2;
  725. inode = gfs2_inode_lookup(dir->i_sb, inum.no_addr, IF2DT(mode));
  726. if (IS_ERR(inode))
  727. goto fail_gunlock2;
  728. error = gfs2_inode_refresh(GFS2_I(inode));
  729. if (error)
  730. goto fail_iput;
  731. error = gfs2_acl_create(dip, GFS2_I(inode));
  732. if (error)
  733. goto fail_iput;
  734. error = gfs2_security_init(dip, GFS2_I(inode));
  735. if (error)
  736. goto fail_iput;
  737. error = link_dinode(dip, name, GFS2_I(inode));
  738. if (error)
  739. goto fail_iput;
  740. if (!inode)
  741. return ERR_PTR(-ENOMEM);
  742. return inode;
  743. fail_iput:
  744. iput(inode);
  745. fail_gunlock2:
  746. gfs2_glock_dq_uninit(ghs + 1);
  747. fail_gunlock:
  748. gfs2_glock_dq(ghs);
  749. fail:
  750. return ERR_PTR(error);
  751. }
  752. /**
  753. * gfs2_rmdiri - Remove a directory
  754. * @dip: The parent directory of the directory to be removed
  755. * @name: The name of the directory to be removed
  756. * @ip: The GFS2 inode of the directory to be removed
  757. *
  758. * Assumes Glocks on dip and ip are held
  759. *
  760. * Returns: errno
  761. */
  762. int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
  763. struct gfs2_inode *ip)
  764. {
  765. struct qstr dotname;
  766. int error;
  767. if (ip->i_di.di_entries != 2) {
  768. if (gfs2_consist_inode(ip))
  769. gfs2_dinode_print(ip);
  770. return -EIO;
  771. }
  772. error = gfs2_dir_del(dip, name);
  773. if (error)
  774. return error;
  775. error = gfs2_change_nlink(dip, -1);
  776. if (error)
  777. return error;
  778. gfs2_str2qstr(&dotname, ".");
  779. error = gfs2_dir_del(ip, &dotname);
  780. if (error)
  781. return error;
  782. gfs2_str2qstr(&dotname, "..");
  783. error = gfs2_dir_del(ip, &dotname);
  784. if (error)
  785. return error;
  786. /* It looks odd, but it really should be done twice */
  787. error = gfs2_change_nlink(ip, -1);
  788. if (error)
  789. return error;
  790. error = gfs2_change_nlink(ip, -1);
  791. if (error)
  792. return error;
  793. return error;
  794. }
  795. /*
  796. * gfs2_unlink_ok - check to see that a inode is still in a directory
  797. * @dip: the directory
  798. * @name: the name of the file
  799. * @ip: the inode
  800. *
  801. * Assumes that the lock on (at least) @dip is held.
  802. *
  803. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  804. */
  805. int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  806. const struct gfs2_inode *ip)
  807. {
  808. int error;
  809. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  810. return -EPERM;
  811. if ((dip->i_inode.i_mode & S_ISVTX) &&
  812. dip->i_inode.i_uid != current->fsuid &&
  813. ip->i_inode.i_uid != current->fsuid && !capable(CAP_FOWNER))
  814. return -EPERM;
  815. if (IS_APPEND(&dip->i_inode))
  816. return -EPERM;
  817. error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
  818. if (error)
  819. return error;
  820. error = gfs2_dir_check(&dip->i_inode, name, ip);
  821. if (error)
  822. return error;
  823. return 0;
  824. }
  825. /*
  826. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  827. * @this: move this
  828. * @to: to here
  829. *
  830. * Follow @to back to the root and make sure we don't encounter @this
  831. * Assumes we already hold the rename lock.
  832. *
  833. * Returns: errno
  834. */
  835. int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  836. {
  837. struct inode *dir = &to->i_inode;
  838. struct super_block *sb = dir->i_sb;
  839. struct inode *tmp;
  840. struct qstr dotdot;
  841. int error = 0;
  842. gfs2_str2qstr(&dotdot, "..");
  843. igrab(dir);
  844. for (;;) {
  845. if (dir == &this->i_inode) {
  846. error = -EINVAL;
  847. break;
  848. }
  849. if (dir == sb->s_root->d_inode) {
  850. error = 0;
  851. break;
  852. }
  853. tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
  854. if (IS_ERR(tmp)) {
  855. error = PTR_ERR(tmp);
  856. break;
  857. }
  858. iput(dir);
  859. dir = tmp;
  860. }
  861. iput(dir);
  862. return error;
  863. }
  864. /**
  865. * gfs2_readlinki - return the contents of a symlink
  866. * @ip: the symlink's inode
  867. * @buf: a pointer to the buffer to be filled
  868. * @len: a pointer to the length of @buf
  869. *
  870. * If @buf is too small, a piece of memory is kmalloc()ed and needs
  871. * to be freed by the caller.
  872. *
  873. * Returns: errno
  874. */
  875. int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
  876. {
  877. struct gfs2_holder i_gh;
  878. struct buffer_head *dibh;
  879. unsigned int x;
  880. int error;
  881. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
  882. error = gfs2_glock_nq_atime(&i_gh);
  883. if (error) {
  884. gfs2_holder_uninit(&i_gh);
  885. return error;
  886. }
  887. if (!ip->i_di.di_size) {
  888. gfs2_consist_inode(ip);
  889. error = -EIO;
  890. goto out;
  891. }
  892. error = gfs2_meta_inode_buffer(ip, &dibh);
  893. if (error)
  894. goto out;
  895. x = ip->i_di.di_size + 1;
  896. if (x > *len) {
  897. *buf = kmalloc(x, GFP_KERNEL);
  898. if (!*buf) {
  899. error = -ENOMEM;
  900. goto out_brelse;
  901. }
  902. }
  903. memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
  904. *len = x;
  905. out_brelse:
  906. brelse(dibh);
  907. out:
  908. gfs2_glock_dq_uninit(&i_gh);
  909. return error;
  910. }
  911. /**
  912. * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
  913. * conditionally update the inode's atime
  914. * @gh: the holder to acquire
  915. *
  916. * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
  917. * Update if the difference between the current time and the inode's current
  918. * atime is greater than an interval specified at mount.
  919. *
  920. * Returns: errno
  921. */
  922. int gfs2_glock_nq_atime(struct gfs2_holder *gh)
  923. {
  924. struct gfs2_glock *gl = gh->gh_gl;
  925. struct gfs2_sbd *sdp = gl->gl_sbd;
  926. struct gfs2_inode *ip = gl->gl_object;
  927. s64 curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
  928. unsigned int state;
  929. int flags;
  930. int error;
  931. if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
  932. gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
  933. gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
  934. return -EINVAL;
  935. state = gh->gh_state;
  936. flags = gh->gh_flags;
  937. error = gfs2_glock_nq(gh);
  938. if (error)
  939. return error;
  940. if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
  941. (sdp->sd_vfs->s_flags & MS_RDONLY))
  942. return 0;
  943. curtime = get_seconds();
  944. if (curtime - ip->i_inode.i_atime.tv_sec >= quantum) {
  945. gfs2_glock_dq(gh);
  946. gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
  947. gh);
  948. error = gfs2_glock_nq(gh);
  949. if (error)
  950. return error;
  951. /* Verify that atime hasn't been updated while we were
  952. trying to get exclusive lock. */
  953. curtime = get_seconds();
  954. if (curtime - ip->i_inode.i_atime.tv_sec >= quantum) {
  955. struct buffer_head *dibh;
  956. struct gfs2_dinode *di;
  957. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  958. if (error == -EROFS)
  959. return 0;
  960. if (error)
  961. goto fail;
  962. error = gfs2_meta_inode_buffer(ip, &dibh);
  963. if (error)
  964. goto fail_end_trans;
  965. ip->i_inode.i_atime.tv_sec = curtime;
  966. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  967. di = (struct gfs2_dinode *)dibh->b_data;
  968. di->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
  969. brelse(dibh);
  970. gfs2_trans_end(sdp);
  971. }
  972. /* If someone else has asked for the glock,
  973. unlock and let them have it. Then reacquire
  974. in the original state. */
  975. if (gfs2_glock_is_blocking(gl)) {
  976. gfs2_glock_dq(gh);
  977. gfs2_holder_reinit(state, flags, gh);
  978. return gfs2_glock_nq(gh);
  979. }
  980. }
  981. return 0;
  982. fail_end_trans:
  983. gfs2_trans_end(sdp);
  984. fail:
  985. gfs2_glock_dq(gh);
  986. return error;
  987. }
  988. static int
  989. __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  990. {
  991. struct buffer_head *dibh;
  992. int error;
  993. error = gfs2_meta_inode_buffer(ip, &dibh);
  994. if (!error) {
  995. error = inode_setattr(&ip->i_inode, attr);
  996. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  997. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  998. gfs2_dinode_out(ip, dibh->b_data);
  999. brelse(dibh);
  1000. }
  1001. return error;
  1002. }
  1003. /**
  1004. * gfs2_setattr_simple -
  1005. * @ip:
  1006. * @attr:
  1007. *
  1008. * Called with a reference on the vnode.
  1009. *
  1010. * Returns: errno
  1011. */
  1012. int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1013. {
  1014. int error;
  1015. if (current->journal_info)
  1016. return __gfs2_setattr_simple(ip, attr);
  1017. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
  1018. if (error)
  1019. return error;
  1020. error = __gfs2_setattr_simple(ip, attr);
  1021. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  1022. return error;
  1023. }