ops_inode.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/namei.h>
  14. #include <linux/utsname.h>
  15. #include <linux/mm.h>
  16. #include <linux/xattr.h>
  17. #include <linux/posix_acl.h>
  18. #include <linux/gfs2_ondisk.h>
  19. #include <linux/crc32.h>
  20. #include <linux/lm_interface.h>
  21. #include <asm/uaccess.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "acl.h"
  25. #include "bmap.h"
  26. #include "dir.h"
  27. #include "eaops.h"
  28. #include "eattr.h"
  29. #include "glock.h"
  30. #include "inode.h"
  31. #include "meta_io.h"
  32. #include "ops_dentry.h"
  33. #include "ops_inode.h"
  34. #include "quota.h"
  35. #include "rgrp.h"
  36. #include "trans.h"
  37. #include "util.h"
  38. /**
  39. * gfs2_create - Create a file
  40. * @dir: The directory in which to create the file
  41. * @dentry: The dentry of the new file
  42. * @mode: The mode of the new file
  43. *
  44. * Returns: errno
  45. */
  46. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  47. int mode, struct nameidata *nd)
  48. {
  49. struct gfs2_inode *dip = GFS2_I(dir);
  50. struct gfs2_sbd *sdp = GFS2_SB(dir);
  51. struct gfs2_holder ghs[2];
  52. struct inode *inode;
  53. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  54. for (;;) {
  55. inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
  56. if (!IS_ERR(inode)) {
  57. gfs2_trans_end(sdp);
  58. if (dip->i_alloc->al_rgd)
  59. gfs2_inplace_release(dip);
  60. gfs2_quota_unlock(dip);
  61. gfs2_alloc_put(dip);
  62. gfs2_glock_dq_uninit_m(2, ghs);
  63. mark_inode_dirty(inode);
  64. break;
  65. } else if (PTR_ERR(inode) != -EEXIST ||
  66. (nd && (nd->intent.open.flags & O_EXCL))) {
  67. gfs2_holder_uninit(ghs);
  68. return PTR_ERR(inode);
  69. }
  70. inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
  71. if (inode) {
  72. if (!IS_ERR(inode)) {
  73. gfs2_holder_uninit(ghs);
  74. break;
  75. } else {
  76. gfs2_holder_uninit(ghs);
  77. return PTR_ERR(inode);
  78. }
  79. }
  80. }
  81. d_instantiate(dentry, inode);
  82. return 0;
  83. }
  84. /**
  85. * gfs2_lookup - Look up a filename in a directory and return its inode
  86. * @dir: The directory inode
  87. * @dentry: The dentry of the new inode
  88. * @nd: passed from Linux VFS, ignored by us
  89. *
  90. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  91. *
  92. * Returns: errno
  93. */
  94. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  95. struct nameidata *nd)
  96. {
  97. struct inode *inode = NULL;
  98. dentry->d_op = &gfs2_dops;
  99. inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
  100. if (inode && IS_ERR(inode))
  101. return ERR_CAST(inode);
  102. if (inode) {
  103. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  104. struct gfs2_holder gh;
  105. int error;
  106. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  107. if (error) {
  108. iput(inode);
  109. return ERR_PTR(error);
  110. }
  111. gfs2_glock_dq_uninit(&gh);
  112. return d_splice_alias(inode, dentry);
  113. }
  114. d_add(dentry, inode);
  115. return NULL;
  116. }
  117. /**
  118. * gfs2_link - Link to a file
  119. * @old_dentry: The inode to link
  120. * @dir: Add link to this directory
  121. * @dentry: The name of the link
  122. *
  123. * Link the inode in "old_dentry" into the directory "dir" with the
  124. * name in "dentry".
  125. *
  126. * Returns: errno
  127. */
  128. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  129. struct dentry *dentry)
  130. {
  131. struct gfs2_inode *dip = GFS2_I(dir);
  132. struct gfs2_sbd *sdp = GFS2_SB(dir);
  133. struct inode *inode = old_dentry->d_inode;
  134. struct gfs2_inode *ip = GFS2_I(inode);
  135. struct gfs2_holder ghs[2];
  136. int alloc_required;
  137. int error;
  138. if (S_ISDIR(inode->i_mode))
  139. return -EPERM;
  140. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  141. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  142. error = gfs2_glock_nq_m(2, ghs);
  143. if (error)
  144. goto out;
  145. error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
  146. if (error)
  147. goto out_gunlock;
  148. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  149. switch (error) {
  150. case -ENOENT:
  151. break;
  152. case 0:
  153. error = -EEXIST;
  154. default:
  155. goto out_gunlock;
  156. }
  157. error = -EINVAL;
  158. if (!dip->i_inode.i_nlink)
  159. goto out_gunlock;
  160. error = -EFBIG;
  161. if (dip->i_di.di_entries == (u32)-1)
  162. goto out_gunlock;
  163. error = -EPERM;
  164. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  165. goto out_gunlock;
  166. error = -EINVAL;
  167. if (!ip->i_inode.i_nlink)
  168. goto out_gunlock;
  169. error = -EMLINK;
  170. if (ip->i_inode.i_nlink == (u32)-1)
  171. goto out_gunlock;
  172. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  173. if (error < 0)
  174. goto out_gunlock;
  175. error = 0;
  176. if (alloc_required) {
  177. struct gfs2_alloc *al = gfs2_alloc_get(dip);
  178. if (!al) {
  179. error = -ENOMEM;
  180. goto out_gunlock;
  181. }
  182. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  183. if (error)
  184. goto out_alloc;
  185. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  186. if (error)
  187. goto out_gunlock_q;
  188. al->al_requested = sdp->sd_max_dirres;
  189. error = gfs2_inplace_reserve(dip);
  190. if (error)
  191. goto out_gunlock_q;
  192. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  193. al->al_rgd->rd_length +
  194. 2 * RES_DINODE + RES_STATFS +
  195. RES_QUOTA, 0);
  196. if (error)
  197. goto out_ipres;
  198. } else {
  199. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  200. if (error)
  201. goto out_ipres;
  202. }
  203. error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
  204. if (error)
  205. goto out_end_trans;
  206. error = gfs2_change_nlink(ip, +1);
  207. out_end_trans:
  208. gfs2_trans_end(sdp);
  209. out_ipres:
  210. if (alloc_required)
  211. gfs2_inplace_release(dip);
  212. out_gunlock_q:
  213. if (alloc_required)
  214. gfs2_quota_unlock(dip);
  215. out_alloc:
  216. if (alloc_required)
  217. gfs2_alloc_put(dip);
  218. out_gunlock:
  219. gfs2_glock_dq_m(2, ghs);
  220. out:
  221. gfs2_holder_uninit(ghs);
  222. gfs2_holder_uninit(ghs + 1);
  223. if (!error) {
  224. atomic_inc(&inode->i_count);
  225. d_instantiate(dentry, inode);
  226. mark_inode_dirty(inode);
  227. }
  228. return error;
  229. }
  230. /**
  231. * gfs2_unlink - Unlink a file
  232. * @dir: The inode of the directory containing the file to unlink
  233. * @dentry: The file itself
  234. *
  235. * Unlink a file. Call gfs2_unlinki()
  236. *
  237. * Returns: errno
  238. */
  239. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  240. {
  241. struct gfs2_inode *dip = GFS2_I(dir);
  242. struct gfs2_sbd *sdp = GFS2_SB(dir);
  243. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  244. struct gfs2_holder ghs[3];
  245. struct gfs2_rgrpd *rgd;
  246. struct gfs2_holder ri_gh;
  247. int error;
  248. error = gfs2_rindex_hold(sdp, &ri_gh);
  249. if (error)
  250. return error;
  251. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  252. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  253. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  254. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  255. error = gfs2_glock_nq(ghs); /* parent */
  256. if (error)
  257. goto out_parent;
  258. error = gfs2_glock_nq(ghs + 1); /* child */
  259. if (error)
  260. goto out_child;
  261. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  262. if (error)
  263. goto out_rgrp;
  264. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  265. if (error)
  266. goto out_rgrp;
  267. error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
  268. if (error)
  269. goto out_rgrp;
  270. error = gfs2_dir_del(dip, &dentry->d_name);
  271. if (error)
  272. goto out_end_trans;
  273. error = gfs2_change_nlink(ip, -1);
  274. out_end_trans:
  275. gfs2_trans_end(sdp);
  276. gfs2_glock_dq(ghs + 2);
  277. out_rgrp:
  278. gfs2_holder_uninit(ghs + 2);
  279. gfs2_glock_dq(ghs + 1);
  280. out_child:
  281. gfs2_holder_uninit(ghs + 1);
  282. gfs2_glock_dq(ghs);
  283. out_parent:
  284. gfs2_holder_uninit(ghs);
  285. gfs2_glock_dq_uninit(&ri_gh);
  286. return error;
  287. }
  288. /**
  289. * gfs2_symlink - Create a symlink
  290. * @dir: The directory to create the symlink in
  291. * @dentry: The dentry to put the symlink in
  292. * @symname: The thing which the link points to
  293. *
  294. * Returns: errno
  295. */
  296. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  297. const char *symname)
  298. {
  299. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  300. struct gfs2_sbd *sdp = GFS2_SB(dir);
  301. struct gfs2_holder ghs[2];
  302. struct inode *inode;
  303. struct buffer_head *dibh;
  304. int size;
  305. int error;
  306. /* Must be stuffed with a null terminator for gfs2_follow_link() */
  307. size = strlen(symname);
  308. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  309. return -ENAMETOOLONG;
  310. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  311. inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
  312. if (IS_ERR(inode)) {
  313. gfs2_holder_uninit(ghs);
  314. return PTR_ERR(inode);
  315. }
  316. ip = ghs[1].gh_gl->gl_object;
  317. ip->i_di.di_size = size;
  318. error = gfs2_meta_inode_buffer(ip, &dibh);
  319. if (!gfs2_assert_withdraw(sdp, !error)) {
  320. gfs2_dinode_out(ip, dibh->b_data);
  321. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
  322. size);
  323. brelse(dibh);
  324. }
  325. gfs2_trans_end(sdp);
  326. if (dip->i_alloc->al_rgd)
  327. gfs2_inplace_release(dip);
  328. gfs2_quota_unlock(dip);
  329. gfs2_alloc_put(dip);
  330. gfs2_glock_dq_uninit_m(2, ghs);
  331. d_instantiate(dentry, inode);
  332. mark_inode_dirty(inode);
  333. return 0;
  334. }
  335. /**
  336. * gfs2_mkdir - Make a directory
  337. * @dir: The parent directory of the new one
  338. * @dentry: The dentry of the new directory
  339. * @mode: The mode of the new directory
  340. *
  341. * Returns: errno
  342. */
  343. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  344. {
  345. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  346. struct gfs2_sbd *sdp = GFS2_SB(dir);
  347. struct gfs2_holder ghs[2];
  348. struct inode *inode;
  349. struct buffer_head *dibh;
  350. int error;
  351. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  352. inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
  353. if (IS_ERR(inode)) {
  354. gfs2_holder_uninit(ghs);
  355. return PTR_ERR(inode);
  356. }
  357. ip = ghs[1].gh_gl->gl_object;
  358. ip->i_inode.i_nlink = 2;
  359. ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
  360. ip->i_di.di_flags |= GFS2_DIF_JDATA;
  361. ip->i_di.di_entries = 2;
  362. error = gfs2_meta_inode_buffer(ip, &dibh);
  363. if (!gfs2_assert_withdraw(sdp, !error)) {
  364. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  365. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  366. struct qstr str;
  367. gfs2_str2qstr(&str, ".");
  368. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  369. gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
  370. dent->de_inum = di->di_num; /* already GFS2 endian */
  371. dent->de_type = cpu_to_be16(DT_DIR);
  372. di->di_entries = cpu_to_be32(1);
  373. gfs2_str2qstr(&str, "..");
  374. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  375. gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  376. gfs2_inum_out(dip, dent);
  377. dent->de_type = cpu_to_be16(DT_DIR);
  378. gfs2_dinode_out(ip, di);
  379. brelse(dibh);
  380. }
  381. error = gfs2_change_nlink(dip, +1);
  382. gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
  383. gfs2_trans_end(sdp);
  384. if (dip->i_alloc->al_rgd)
  385. gfs2_inplace_release(dip);
  386. gfs2_quota_unlock(dip);
  387. gfs2_alloc_put(dip);
  388. gfs2_glock_dq_uninit_m(2, ghs);
  389. d_instantiate(dentry, inode);
  390. mark_inode_dirty(inode);
  391. return 0;
  392. }
  393. /**
  394. * gfs2_rmdir - Remove a directory
  395. * @dir: The parent directory of the directory to be removed
  396. * @dentry: The dentry of the directory to remove
  397. *
  398. * Remove a directory. Call gfs2_rmdiri()
  399. *
  400. * Returns: errno
  401. */
  402. static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
  403. {
  404. struct gfs2_inode *dip = GFS2_I(dir);
  405. struct gfs2_sbd *sdp = GFS2_SB(dir);
  406. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  407. struct gfs2_holder ghs[3];
  408. struct gfs2_rgrpd *rgd;
  409. struct gfs2_holder ri_gh;
  410. int error;
  411. error = gfs2_rindex_hold(sdp, &ri_gh);
  412. if (error)
  413. return error;
  414. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  415. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  416. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  417. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  418. error = gfs2_glock_nq_m(3, ghs);
  419. if (error)
  420. goto out;
  421. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  422. if (error)
  423. goto out_gunlock;
  424. if (ip->i_di.di_entries < 2) {
  425. if (gfs2_consist_inode(ip))
  426. gfs2_dinode_print(ip);
  427. error = -EIO;
  428. goto out_gunlock;
  429. }
  430. if (ip->i_di.di_entries > 2) {
  431. error = -ENOTEMPTY;
  432. goto out_gunlock;
  433. }
  434. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
  435. if (error)
  436. goto out_gunlock;
  437. error = gfs2_rmdiri(dip, &dentry->d_name, ip);
  438. gfs2_trans_end(sdp);
  439. out_gunlock:
  440. gfs2_glock_dq_m(3, ghs);
  441. out:
  442. gfs2_holder_uninit(ghs);
  443. gfs2_holder_uninit(ghs + 1);
  444. gfs2_holder_uninit(ghs + 2);
  445. gfs2_glock_dq_uninit(&ri_gh);
  446. return error;
  447. }
  448. /**
  449. * gfs2_mknod - Make a special file
  450. * @dir: The directory in which the special file will reside
  451. * @dentry: The dentry of the special file
  452. * @mode: The mode of the special file
  453. * @rdev: The device specification of the special file
  454. *
  455. */
  456. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  457. dev_t dev)
  458. {
  459. struct gfs2_inode *dip = GFS2_I(dir);
  460. struct gfs2_sbd *sdp = GFS2_SB(dir);
  461. struct gfs2_holder ghs[2];
  462. struct inode *inode;
  463. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  464. inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
  465. if (IS_ERR(inode)) {
  466. gfs2_holder_uninit(ghs);
  467. return PTR_ERR(inode);
  468. }
  469. gfs2_trans_end(sdp);
  470. if (dip->i_alloc->al_rgd)
  471. gfs2_inplace_release(dip);
  472. gfs2_quota_unlock(dip);
  473. gfs2_alloc_put(dip);
  474. gfs2_glock_dq_uninit_m(2, ghs);
  475. d_instantiate(dentry, inode);
  476. mark_inode_dirty(inode);
  477. return 0;
  478. }
  479. /**
  480. * gfs2_rename - Rename a file
  481. * @odir: Parent directory of old file name
  482. * @odentry: The old dentry of the file
  483. * @ndir: Parent directory of new file name
  484. * @ndentry: The new dentry of the file
  485. *
  486. * Returns: errno
  487. */
  488. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  489. struct inode *ndir, struct dentry *ndentry)
  490. {
  491. struct gfs2_inode *odip = GFS2_I(odir);
  492. struct gfs2_inode *ndip = GFS2_I(ndir);
  493. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  494. struct gfs2_inode *nip = NULL;
  495. struct gfs2_sbd *sdp = GFS2_SB(odir);
  496. struct gfs2_holder ghs[5], r_gh;
  497. struct gfs2_rgrpd *nrgd;
  498. unsigned int num_gh;
  499. int dir_rename = 0;
  500. int alloc_required;
  501. unsigned int x;
  502. int error;
  503. if (ndentry->d_inode) {
  504. nip = GFS2_I(ndentry->d_inode);
  505. if (ip == nip)
  506. return 0;
  507. }
  508. /* Make sure we aren't trying to move a dirctory into it's subdir */
  509. if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
  510. dir_rename = 1;
  511. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
  512. &r_gh);
  513. if (error)
  514. goto out;
  515. error = gfs2_ok_to_move(ip, ndip);
  516. if (error)
  517. goto out_gunlock_r;
  518. }
  519. num_gh = 1;
  520. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  521. if (odip != ndip) {
  522. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  523. num_gh++;
  524. }
  525. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  526. num_gh++;
  527. if (nip) {
  528. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  529. num_gh++;
  530. /* grab the resource lock for unlink flag twiddling
  531. * this is the case of the target file already existing
  532. * so we unlink before doing the rename
  533. */
  534. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  535. if (nrgd)
  536. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  537. }
  538. error = gfs2_glock_nq_m(num_gh, ghs);
  539. if (error)
  540. goto out_uninit;
  541. /* Check out the old directory */
  542. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  543. if (error)
  544. goto out_gunlock;
  545. /* Check out the new directory */
  546. if (nip) {
  547. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  548. if (error)
  549. goto out_gunlock;
  550. if (S_ISDIR(nip->i_inode.i_mode)) {
  551. if (nip->i_di.di_entries < 2) {
  552. if (gfs2_consist_inode(nip))
  553. gfs2_dinode_print(nip);
  554. error = -EIO;
  555. goto out_gunlock;
  556. }
  557. if (nip->i_di.di_entries > 2) {
  558. error = -ENOTEMPTY;
  559. goto out_gunlock;
  560. }
  561. }
  562. } else {
  563. error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
  564. if (error)
  565. goto out_gunlock;
  566. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  567. switch (error) {
  568. case -ENOENT:
  569. error = 0;
  570. break;
  571. case 0:
  572. error = -EEXIST;
  573. default:
  574. goto out_gunlock;
  575. };
  576. if (odip != ndip) {
  577. if (!ndip->i_inode.i_nlink) {
  578. error = -EINVAL;
  579. goto out_gunlock;
  580. }
  581. if (ndip->i_di.di_entries == (u32)-1) {
  582. error = -EFBIG;
  583. goto out_gunlock;
  584. }
  585. if (S_ISDIR(ip->i_inode.i_mode) &&
  586. ndip->i_inode.i_nlink == (u32)-1) {
  587. error = -EMLINK;
  588. goto out_gunlock;
  589. }
  590. }
  591. }
  592. /* Check out the dir to be renamed */
  593. if (dir_rename) {
  594. error = permission(odentry->d_inode, MAY_WRITE, NULL);
  595. if (error)
  596. goto out_gunlock;
  597. }
  598. alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  599. if (error < 0)
  600. goto out_gunlock;
  601. error = 0;
  602. if (alloc_required) {
  603. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  604. if (!al) {
  605. error = -ENOMEM;
  606. goto out_gunlock;
  607. }
  608. error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  609. if (error)
  610. goto out_alloc;
  611. error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid);
  612. if (error)
  613. goto out_gunlock_q;
  614. al->al_requested = sdp->sd_max_dirres;
  615. error = gfs2_inplace_reserve(ndip);
  616. if (error)
  617. goto out_gunlock_q;
  618. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  619. al->al_rgd->rd_length +
  620. 4 * RES_DINODE + 4 * RES_LEAF +
  621. RES_STATFS + RES_QUOTA + 4, 0);
  622. if (error)
  623. goto out_ipreserv;
  624. } else {
  625. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  626. 5 * RES_LEAF + 4, 0);
  627. if (error)
  628. goto out_gunlock;
  629. }
  630. /* Remove the target file, if it exists */
  631. if (nip) {
  632. if (S_ISDIR(nip->i_inode.i_mode))
  633. error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
  634. else {
  635. error = gfs2_dir_del(ndip, &ndentry->d_name);
  636. if (error)
  637. goto out_end_trans;
  638. error = gfs2_change_nlink(nip, -1);
  639. }
  640. if (error)
  641. goto out_end_trans;
  642. }
  643. if (dir_rename) {
  644. struct qstr name;
  645. gfs2_str2qstr(&name, "..");
  646. error = gfs2_change_nlink(ndip, +1);
  647. if (error)
  648. goto out_end_trans;
  649. error = gfs2_change_nlink(odip, -1);
  650. if (error)
  651. goto out_end_trans;
  652. error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
  653. if (error)
  654. goto out_end_trans;
  655. } else {
  656. struct buffer_head *dibh;
  657. error = gfs2_meta_inode_buffer(ip, &dibh);
  658. if (error)
  659. goto out_end_trans;
  660. ip->i_inode.i_ctime = CURRENT_TIME;
  661. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  662. gfs2_dinode_out(ip, dibh->b_data);
  663. brelse(dibh);
  664. }
  665. error = gfs2_dir_del(odip, &odentry->d_name);
  666. if (error)
  667. goto out_end_trans;
  668. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
  669. if (error)
  670. goto out_end_trans;
  671. out_end_trans:
  672. gfs2_trans_end(sdp);
  673. out_ipreserv:
  674. if (alloc_required)
  675. gfs2_inplace_release(ndip);
  676. out_gunlock_q:
  677. if (alloc_required)
  678. gfs2_quota_unlock(ndip);
  679. out_alloc:
  680. if (alloc_required)
  681. gfs2_alloc_put(ndip);
  682. out_gunlock:
  683. gfs2_glock_dq_m(num_gh, ghs);
  684. out_uninit:
  685. for (x = 0; x < num_gh; x++)
  686. gfs2_holder_uninit(ghs + x);
  687. out_gunlock_r:
  688. if (dir_rename)
  689. gfs2_glock_dq_uninit(&r_gh);
  690. out:
  691. return error;
  692. }
  693. /**
  694. * gfs2_readlink - Read the value of a symlink
  695. * @dentry: the symlink
  696. * @buf: the buffer to read the symlink data into
  697. * @size: the size of the buffer
  698. *
  699. * Returns: errno
  700. */
  701. static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
  702. int user_size)
  703. {
  704. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  705. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  706. unsigned int len = GFS2_FAST_NAME_SIZE;
  707. int error;
  708. error = gfs2_readlinki(ip, &buf, &len);
  709. if (error)
  710. return error;
  711. if (user_size > len - 1)
  712. user_size = len - 1;
  713. if (copy_to_user(user_buf, buf, user_size))
  714. error = -EFAULT;
  715. else
  716. error = user_size;
  717. if (buf != array)
  718. kfree(buf);
  719. return error;
  720. }
  721. /**
  722. * gfs2_follow_link - Follow a symbolic link
  723. * @dentry: The dentry of the link
  724. * @nd: Data that we pass to vfs_follow_link()
  725. *
  726. * This can handle symlinks of any size. It is optimised for symlinks
  727. * under GFS2_FAST_NAME_SIZE.
  728. *
  729. * Returns: 0 on success or error code
  730. */
  731. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  732. {
  733. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  734. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  735. unsigned int len = GFS2_FAST_NAME_SIZE;
  736. int error;
  737. error = gfs2_readlinki(ip, &buf, &len);
  738. if (!error) {
  739. error = vfs_follow_link(nd, buf);
  740. if (buf != array)
  741. kfree(buf);
  742. }
  743. return ERR_PTR(error);
  744. }
  745. /**
  746. * gfs2_permission -
  747. * @inode:
  748. * @mask:
  749. * @nd: passed from Linux VFS, ignored by us
  750. *
  751. * This may be called from the VFS directly, or from within GFS2 with the
  752. * inode locked, so we look to see if the glock is already locked and only
  753. * lock the glock if its not already been done.
  754. *
  755. * Returns: errno
  756. */
  757. static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
  758. {
  759. struct gfs2_inode *ip = GFS2_I(inode);
  760. struct gfs2_holder i_gh;
  761. int error;
  762. int unlock = 0;
  763. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  764. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  765. if (error)
  766. return error;
  767. unlock = 1;
  768. }
  769. error = generic_permission(inode, mask, gfs2_check_acl);
  770. if (unlock)
  771. gfs2_glock_dq_uninit(&i_gh);
  772. return error;
  773. }
  774. static int setattr_size(struct inode *inode, struct iattr *attr)
  775. {
  776. struct gfs2_inode *ip = GFS2_I(inode);
  777. struct gfs2_sbd *sdp = GFS2_SB(inode);
  778. int error;
  779. if (attr->ia_size != ip->i_di.di_size) {
  780. error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
  781. if (error)
  782. return error;
  783. error = vmtruncate(inode, attr->ia_size);
  784. gfs2_trans_end(sdp);
  785. if (error)
  786. return error;
  787. }
  788. error = gfs2_truncatei(ip, attr->ia_size);
  789. if (error && (inode->i_size != ip->i_di.di_size))
  790. i_size_write(inode, ip->i_di.di_size);
  791. return error;
  792. }
  793. static int setattr_chown(struct inode *inode, struct iattr *attr)
  794. {
  795. struct gfs2_inode *ip = GFS2_I(inode);
  796. struct gfs2_sbd *sdp = GFS2_SB(inode);
  797. struct buffer_head *dibh;
  798. u32 ouid, ogid, nuid, ngid;
  799. int error;
  800. ouid = inode->i_uid;
  801. ogid = inode->i_gid;
  802. nuid = attr->ia_uid;
  803. ngid = attr->ia_gid;
  804. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  805. ouid = nuid = NO_QUOTA_CHANGE;
  806. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  807. ogid = ngid = NO_QUOTA_CHANGE;
  808. if (!gfs2_alloc_get(ip))
  809. return -ENOMEM;
  810. error = gfs2_quota_lock(ip, nuid, ngid);
  811. if (error)
  812. goto out_alloc;
  813. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  814. error = gfs2_quota_check(ip, nuid, ngid);
  815. if (error)
  816. goto out_gunlock_q;
  817. }
  818. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  819. if (error)
  820. goto out_gunlock_q;
  821. error = gfs2_meta_inode_buffer(ip, &dibh);
  822. if (error)
  823. goto out_end_trans;
  824. error = inode_setattr(inode, attr);
  825. gfs2_assert_warn(sdp, !error);
  826. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  827. gfs2_dinode_out(ip, dibh->b_data);
  828. brelse(dibh);
  829. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  830. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  831. gfs2_quota_change(ip, -blocks, ouid, ogid);
  832. gfs2_quota_change(ip, blocks, nuid, ngid);
  833. }
  834. out_end_trans:
  835. gfs2_trans_end(sdp);
  836. out_gunlock_q:
  837. gfs2_quota_unlock(ip);
  838. out_alloc:
  839. gfs2_alloc_put(ip);
  840. return error;
  841. }
  842. /**
  843. * gfs2_setattr - Change attributes on an inode
  844. * @dentry: The dentry which is changing
  845. * @attr: The structure describing the change
  846. *
  847. * The VFS layer wants to change one or more of an inodes attributes. Write
  848. * that change out to disk.
  849. *
  850. * Returns: errno
  851. */
  852. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  853. {
  854. struct inode *inode = dentry->d_inode;
  855. struct gfs2_inode *ip = GFS2_I(inode);
  856. struct gfs2_holder i_gh;
  857. int error;
  858. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  859. if (error)
  860. return error;
  861. error = -EPERM;
  862. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  863. goto out;
  864. error = inode_change_ok(inode, attr);
  865. if (error)
  866. goto out;
  867. if (attr->ia_valid & ATTR_SIZE)
  868. error = setattr_size(inode, attr);
  869. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  870. error = setattr_chown(inode, attr);
  871. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  872. error = gfs2_acl_chmod(ip, attr);
  873. else
  874. error = gfs2_setattr_simple(ip, attr);
  875. out:
  876. gfs2_glock_dq_uninit(&i_gh);
  877. if (!error)
  878. mark_inode_dirty(inode);
  879. return error;
  880. }
  881. /**
  882. * gfs2_getattr - Read out an inode's attributes
  883. * @mnt: The vfsmount the inode is being accessed from
  884. * @dentry: The dentry to stat
  885. * @stat: The inode's stats
  886. *
  887. * This may be called from the VFS directly, or from within GFS2 with the
  888. * inode locked, so we look to see if the glock is already locked and only
  889. * lock the glock if its not already been done. Note that its the NFS
  890. * readdirplus operation which causes this to be called (from filldir)
  891. * with the glock already held.
  892. *
  893. * Returns: errno
  894. */
  895. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  896. struct kstat *stat)
  897. {
  898. struct inode *inode = dentry->d_inode;
  899. struct gfs2_inode *ip = GFS2_I(inode);
  900. struct gfs2_holder gh;
  901. int error;
  902. int unlock = 0;
  903. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  904. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  905. if (error)
  906. return error;
  907. unlock = 1;
  908. }
  909. generic_fillattr(inode, stat);
  910. if (unlock)
  911. gfs2_glock_dq_uninit(&gh);
  912. return 0;
  913. }
  914. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  915. const void *data, size_t size, int flags)
  916. {
  917. struct inode *inode = dentry->d_inode;
  918. struct gfs2_ea_request er;
  919. memset(&er, 0, sizeof(struct gfs2_ea_request));
  920. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  921. if (er.er_type == GFS2_EATYPE_UNUSED)
  922. return -EOPNOTSUPP;
  923. er.er_data = (char *)data;
  924. er.er_name_len = strlen(er.er_name);
  925. er.er_data_len = size;
  926. er.er_flags = flags;
  927. gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
  928. return gfs2_ea_set(GFS2_I(inode), &er);
  929. }
  930. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  931. void *data, size_t size)
  932. {
  933. struct gfs2_ea_request er;
  934. memset(&er, 0, sizeof(struct gfs2_ea_request));
  935. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  936. if (er.er_type == GFS2_EATYPE_UNUSED)
  937. return -EOPNOTSUPP;
  938. er.er_data = data;
  939. er.er_name_len = strlen(er.er_name);
  940. er.er_data_len = size;
  941. return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
  942. }
  943. static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  944. {
  945. struct gfs2_ea_request er;
  946. memset(&er, 0, sizeof(struct gfs2_ea_request));
  947. er.er_data = (size) ? buffer : NULL;
  948. er.er_data_len = size;
  949. return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
  950. }
  951. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  952. {
  953. struct gfs2_ea_request er;
  954. memset(&er, 0, sizeof(struct gfs2_ea_request));
  955. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  956. if (er.er_type == GFS2_EATYPE_UNUSED)
  957. return -EOPNOTSUPP;
  958. er.er_name_len = strlen(er.er_name);
  959. return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
  960. }
  961. const struct inode_operations gfs2_file_iops = {
  962. .permission = gfs2_permission,
  963. .setattr = gfs2_setattr,
  964. .getattr = gfs2_getattr,
  965. .setxattr = gfs2_setxattr,
  966. .getxattr = gfs2_getxattr,
  967. .listxattr = gfs2_listxattr,
  968. .removexattr = gfs2_removexattr,
  969. };
  970. const struct inode_operations gfs2_dir_iops = {
  971. .create = gfs2_create,
  972. .lookup = gfs2_lookup,
  973. .link = gfs2_link,
  974. .unlink = gfs2_unlink,
  975. .symlink = gfs2_symlink,
  976. .mkdir = gfs2_mkdir,
  977. .rmdir = gfs2_rmdir,
  978. .mknod = gfs2_mknod,
  979. .rename = gfs2_rename,
  980. .permission = gfs2_permission,
  981. .setattr = gfs2_setattr,
  982. .getattr = gfs2_getattr,
  983. .setxattr = gfs2_setxattr,
  984. .getxattr = gfs2_getxattr,
  985. .listxattr = gfs2_listxattr,
  986. .removexattr = gfs2_removexattr,
  987. };
  988. const struct inode_operations gfs2_symlink_iops = {
  989. .readlink = gfs2_readlink,
  990. .follow_link = gfs2_follow_link,
  991. .permission = gfs2_permission,
  992. .setattr = gfs2_setattr,
  993. .getattr = gfs2_getattr,
  994. .setxattr = gfs2_setxattr,
  995. .getxattr = gfs2_getxattr,
  996. .listxattr = gfs2_listxattr,
  997. .removexattr = gfs2_removexattr,
  998. };