inode.c 28 KB

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