ops_inode.c 26 KB

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