inode.c 29 KB

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