inode.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2011 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/mm.h>
  15. #include <linux/xattr.h>
  16. #include <linux/posix_acl.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/crc32.h>
  19. #include <linux/fiemap.h>
  20. #include <linux/security.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 "xattr.h"
  28. #include "glock.h"
  29. #include "inode.h"
  30. #include "meta_io.h"
  31. #include "quota.h"
  32. #include "rgrp.h"
  33. #include "trans.h"
  34. #include "util.h"
  35. #include "super.h"
  36. #include "glops.h"
  37. struct gfs2_skip_data {
  38. u64 no_addr;
  39. int skipped;
  40. int non_block;
  41. };
  42. static int iget_test(struct inode *inode, void *opaque)
  43. {
  44. struct gfs2_inode *ip = GFS2_I(inode);
  45. struct gfs2_skip_data *data = opaque;
  46. if (ip->i_no_addr == data->no_addr) {
  47. if (data->non_block &&
  48. inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
  49. data->skipped = 1;
  50. return 0;
  51. }
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. static int iget_set(struct inode *inode, void *opaque)
  57. {
  58. struct gfs2_inode *ip = GFS2_I(inode);
  59. struct gfs2_skip_data *data = opaque;
  60. if (data->skipped)
  61. return -ENOENT;
  62. inode->i_ino = (unsigned long)(data->no_addr);
  63. ip->i_no_addr = data->no_addr;
  64. return 0;
  65. }
  66. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr, int non_block)
  67. {
  68. unsigned long hash = (unsigned long)no_addr;
  69. struct gfs2_skip_data data;
  70. data.no_addr = no_addr;
  71. data.skipped = 0;
  72. data.non_block = non_block;
  73. return ilookup5(sb, hash, iget_test, &data);
  74. }
  75. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
  76. int non_block)
  77. {
  78. struct gfs2_skip_data data;
  79. unsigned long hash = (unsigned long)no_addr;
  80. data.no_addr = no_addr;
  81. data.skipped = 0;
  82. data.non_block = non_block;
  83. return iget5_locked(sb, hash, iget_test, iget_set, &data);
  84. }
  85. /**
  86. * gfs2_set_iop - Sets inode operations
  87. * @inode: The inode with correct i_mode filled in
  88. *
  89. * GFS2 lookup code fills in vfs inode contents based on info obtained
  90. * from directory entry inside gfs2_inode_lookup().
  91. */
  92. static void gfs2_set_iop(struct inode *inode)
  93. {
  94. struct gfs2_sbd *sdp = GFS2_SB(inode);
  95. umode_t mode = inode->i_mode;
  96. if (S_ISREG(mode)) {
  97. inode->i_op = &gfs2_file_iops;
  98. if (gfs2_localflocks(sdp))
  99. inode->i_fop = &gfs2_file_fops_nolock;
  100. else
  101. inode->i_fop = &gfs2_file_fops;
  102. } else if (S_ISDIR(mode)) {
  103. inode->i_op = &gfs2_dir_iops;
  104. if (gfs2_localflocks(sdp))
  105. inode->i_fop = &gfs2_dir_fops_nolock;
  106. else
  107. inode->i_fop = &gfs2_dir_fops;
  108. } else if (S_ISLNK(mode)) {
  109. inode->i_op = &gfs2_symlink_iops;
  110. } else {
  111. inode->i_op = &gfs2_file_iops;
  112. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  113. }
  114. }
  115. /**
  116. * gfs2_inode_lookup - Lookup an inode
  117. * @sb: The super block
  118. * @no_addr: The inode number
  119. * @type: The type of the inode
  120. * non_block: Can we block on inodes that are being freed?
  121. *
  122. * Returns: A VFS inode, or an error
  123. */
  124. struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
  125. u64 no_addr, u64 no_formal_ino, int non_block)
  126. {
  127. struct inode *inode;
  128. struct gfs2_inode *ip;
  129. struct gfs2_glock *io_gl = NULL;
  130. int error;
  131. inode = gfs2_iget(sb, no_addr, non_block);
  132. ip = GFS2_I(inode);
  133. if (!inode)
  134. return ERR_PTR(-ENOBUFS);
  135. if (inode->i_state & I_NEW) {
  136. struct gfs2_sbd *sdp = GFS2_SB(inode);
  137. ip->i_no_formal_ino = no_formal_ino;
  138. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  139. if (unlikely(error))
  140. goto fail;
  141. ip->i_gl->gl_object = ip;
  142. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  143. if (unlikely(error))
  144. goto fail_put;
  145. set_bit(GIF_INVALID, &ip->i_flags);
  146. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  147. if (unlikely(error))
  148. goto fail_iopen;
  149. ip->i_iopen_gh.gh_gl->gl_object = ip;
  150. gfs2_glock_put(io_gl);
  151. io_gl = NULL;
  152. if (type == DT_UNKNOWN) {
  153. /* Inode glock must be locked already */
  154. error = gfs2_inode_refresh(GFS2_I(inode));
  155. if (error)
  156. goto fail_refresh;
  157. } else {
  158. inode->i_mode = DT2IF(type);
  159. }
  160. gfs2_set_iop(inode);
  161. unlock_new_inode(inode);
  162. }
  163. return inode;
  164. fail_refresh:
  165. ip->i_iopen_gh.gh_gl->gl_object = NULL;
  166. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  167. fail_iopen:
  168. if (io_gl)
  169. gfs2_glock_put(io_gl);
  170. fail_put:
  171. ip->i_gl->gl_object = NULL;
  172. gfs2_glock_put(ip->i_gl);
  173. fail:
  174. iget_failed(inode);
  175. return ERR_PTR(error);
  176. }
  177. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  178. u64 *no_formal_ino, unsigned int blktype)
  179. {
  180. struct super_block *sb = sdp->sd_vfs;
  181. struct gfs2_holder i_gh;
  182. struct inode *inode = NULL;
  183. int error;
  184. /* Must not read in block until block type is verified */
  185. error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
  186. LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
  187. if (error)
  188. return ERR_PTR(error);
  189. error = gfs2_check_blk_type(sdp, no_addr, blktype);
  190. if (error)
  191. goto fail;
  192. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
  193. if (IS_ERR(inode))
  194. goto fail;
  195. /* Two extra checks for NFS only */
  196. if (no_formal_ino) {
  197. error = -ESTALE;
  198. if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
  199. goto fail_iput;
  200. error = -EIO;
  201. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
  202. goto fail_iput;
  203. error = 0;
  204. }
  205. fail:
  206. gfs2_glock_dq_uninit(&i_gh);
  207. return error ? ERR_PTR(error) : inode;
  208. fail_iput:
  209. iput(inode);
  210. goto fail;
  211. }
  212. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  213. {
  214. struct qstr qstr;
  215. struct inode *inode;
  216. gfs2_str2qstr(&qstr, name);
  217. inode = gfs2_lookupi(dip, &qstr, 1);
  218. /* gfs2_lookupi has inconsistent callers: vfs
  219. * related routines expect NULL for no entry found,
  220. * gfs2_lookup_simple callers expect ENOENT
  221. * and do not check for NULL.
  222. */
  223. if (inode == NULL)
  224. return ERR_PTR(-ENOENT);
  225. else
  226. return inode;
  227. }
  228. /**
  229. * gfs2_lookupi - Look up a filename in a directory and return its inode
  230. * @d_gh: An initialized holder for the directory glock
  231. * @name: The name of the inode to look for
  232. * @is_root: If 1, ignore the caller's permissions
  233. * @i_gh: An uninitialized holder for the new inode glock
  234. *
  235. * This can be called via the VFS filldir function when NFS is doing
  236. * a readdirplus and the inode which its intending to stat isn't
  237. * already in cache. In this case we must not take the directory glock
  238. * again, since the readdir call will have already taken that lock.
  239. *
  240. * Returns: errno
  241. */
  242. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  243. int is_root)
  244. {
  245. struct super_block *sb = dir->i_sb;
  246. struct gfs2_inode *dip = GFS2_I(dir);
  247. struct gfs2_holder d_gh;
  248. int error = 0;
  249. struct inode *inode = NULL;
  250. int unlock = 0;
  251. if (!name->len || name->len > GFS2_FNAMESIZE)
  252. return ERR_PTR(-ENAMETOOLONG);
  253. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  254. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  255. dir == sb->s_root->d_inode)) {
  256. igrab(dir);
  257. return dir;
  258. }
  259. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  260. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  261. if (error)
  262. return ERR_PTR(error);
  263. unlock = 1;
  264. }
  265. if (!is_root) {
  266. error = gfs2_permission(dir, MAY_EXEC, 0);
  267. if (error)
  268. goto out;
  269. }
  270. inode = gfs2_dir_search(dir, name);
  271. if (IS_ERR(inode))
  272. error = PTR_ERR(inode);
  273. out:
  274. if (unlock)
  275. gfs2_glock_dq_uninit(&d_gh);
  276. if (error == -ENOENT)
  277. return NULL;
  278. return inode ? inode : ERR_PTR(error);
  279. }
  280. /**
  281. * create_ok - OK to create a new on-disk inode here?
  282. * @dip: Directory in which dinode is to be created
  283. * @name: Name of new dinode
  284. * @mode:
  285. *
  286. * Returns: errno
  287. */
  288. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  289. unsigned int mode)
  290. {
  291. int error;
  292. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
  293. if (error)
  294. return error;
  295. /* Don't create entries in an unlinked directory */
  296. if (!dip->i_inode.i_nlink)
  297. return -ENOENT;
  298. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  299. switch (error) {
  300. case -ENOENT:
  301. error = 0;
  302. break;
  303. case 0:
  304. return -EEXIST;
  305. default:
  306. return error;
  307. }
  308. if (dip->i_entries == (u32)-1)
  309. return -EFBIG;
  310. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  311. return -EMLINK;
  312. return 0;
  313. }
  314. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  315. unsigned int *uid, unsigned int *gid)
  316. {
  317. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  318. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  319. if (S_ISDIR(*mode))
  320. *mode |= S_ISUID;
  321. else if (dip->i_inode.i_uid != current_fsuid())
  322. *mode &= ~07111;
  323. *uid = dip->i_inode.i_uid;
  324. } else
  325. *uid = current_fsuid();
  326. if (dip->i_inode.i_mode & S_ISGID) {
  327. if (S_ISDIR(*mode))
  328. *mode |= S_ISGID;
  329. *gid = dip->i_inode.i_gid;
  330. } else
  331. *gid = current_fsgid();
  332. }
  333. static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
  334. {
  335. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  336. int error;
  337. if (gfs2_alloc_get(dip) == NULL)
  338. return -ENOMEM;
  339. dip->i_alloc->al_requested = RES_DINODE;
  340. error = gfs2_inplace_reserve(dip);
  341. if (error)
  342. goto out;
  343. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  344. if (error)
  345. goto out_ipreserv;
  346. error = gfs2_alloc_di(dip, no_addr, generation);
  347. gfs2_trans_end(sdp);
  348. out_ipreserv:
  349. gfs2_inplace_release(dip);
  350. out:
  351. gfs2_alloc_put(dip);
  352. return error;
  353. }
  354. static void gfs2_init_dir(struct buffer_head *dibh,
  355. const struct gfs2_inode *parent)
  356. {
  357. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  358. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  359. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  360. dent->de_inum = di->di_num; /* already GFS2 endian */
  361. dent->de_type = cpu_to_be16(DT_DIR);
  362. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  363. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  364. gfs2_inum_out(parent, dent);
  365. dent->de_type = cpu_to_be16(DT_DIR);
  366. }
  367. /**
  368. * init_dinode - Fill in a new dinode structure
  369. * @dip: The directory this inode is being created in
  370. * @gl: The glock covering the new inode
  371. * @inum: The inode number
  372. * @mode: The file permissions
  373. * @uid: The uid of the new inode
  374. * @gid: The gid of the new inode
  375. * @generation: The generation number of the new inode
  376. * @dev: The device number (if a device node)
  377. * @symname: The symlink destination (if a symlink)
  378. * @size: The inode size (ignored for directories)
  379. * @bhp: The buffer head (returned to caller)
  380. *
  381. */
  382. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  383. const struct gfs2_inum_host *inum, unsigned int mode,
  384. unsigned int uid, unsigned int gid,
  385. const u64 *generation, dev_t dev, const char *symname,
  386. unsigned size, struct buffer_head **bhp)
  387. {
  388. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  389. struct gfs2_dinode *di;
  390. struct buffer_head *dibh;
  391. struct timespec tv = CURRENT_TIME;
  392. dibh = gfs2_meta_new(gl, inum->no_addr);
  393. gfs2_trans_add_bh(gl, dibh, 1);
  394. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  395. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  396. di = (struct gfs2_dinode *)dibh->b_data;
  397. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  398. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  399. di->di_mode = cpu_to_be32(mode);
  400. di->di_uid = cpu_to_be32(uid);
  401. di->di_gid = cpu_to_be32(gid);
  402. di->di_nlink = 0;
  403. di->di_size = cpu_to_be64(size);
  404. di->di_blocks = cpu_to_be64(1);
  405. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  406. di->di_major = cpu_to_be32(MAJOR(dev));
  407. di->di_minor = cpu_to_be32(MINOR(dev));
  408. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  409. di->di_generation = cpu_to_be64(*generation);
  410. di->di_flags = 0;
  411. di->__pad1 = 0;
  412. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  413. di->di_height = 0;
  414. di->__pad2 = 0;
  415. di->__pad3 = 0;
  416. di->di_depth = 0;
  417. di->di_entries = 0;
  418. memset(&di->__pad4, 0, sizeof(di->__pad4));
  419. di->di_eattr = 0;
  420. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  421. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  422. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  423. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  424. switch(mode & S_IFMT) {
  425. case S_IFREG:
  426. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  427. gfs2_tune_get(sdp, gt_new_files_jdata))
  428. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  429. break;
  430. case S_IFDIR:
  431. di->di_flags |= cpu_to_be32(dip->i_diskflags &
  432. GFS2_DIF_INHERIT_JDATA);
  433. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  434. di->di_size = cpu_to_be64(sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
  435. di->di_entries = cpu_to_be32(2);
  436. gfs2_init_dir(dibh, dip);
  437. break;
  438. case S_IFLNK:
  439. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, size);
  440. break;
  441. }
  442. set_buffer_uptodate(dibh);
  443. *bhp = dibh;
  444. }
  445. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  446. unsigned int mode, const struct gfs2_inum_host *inum,
  447. const u64 *generation, dev_t dev, const char *symname,
  448. unsigned int size, struct buffer_head **bhp)
  449. {
  450. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  451. unsigned int uid, gid;
  452. int error;
  453. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  454. if (!gfs2_alloc_get(dip))
  455. return -ENOMEM;
  456. error = gfs2_quota_lock(dip, uid, gid);
  457. if (error)
  458. goto out;
  459. error = gfs2_quota_check(dip, uid, gid);
  460. if (error)
  461. goto out_quota;
  462. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  463. if (error)
  464. goto out_quota;
  465. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, symname, size, bhp);
  466. gfs2_quota_change(dip, +1, uid, gid);
  467. gfs2_trans_end(sdp);
  468. out_quota:
  469. gfs2_quota_unlock(dip);
  470. out:
  471. gfs2_alloc_put(dip);
  472. return error;
  473. }
  474. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  475. struct gfs2_inode *ip)
  476. {
  477. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  478. struct gfs2_alloc *al;
  479. int alloc_required;
  480. struct buffer_head *dibh;
  481. int error;
  482. al = gfs2_alloc_get(dip);
  483. if (!al)
  484. return -ENOMEM;
  485. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  486. if (error)
  487. goto fail;
  488. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  489. if (alloc_required < 0)
  490. goto fail_quota_locks;
  491. if (alloc_required) {
  492. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  493. if (error)
  494. goto fail_quota_locks;
  495. al->al_requested = sdp->sd_max_dirres;
  496. error = gfs2_inplace_reserve(dip);
  497. if (error)
  498. goto fail_quota_locks;
  499. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  500. al->al_rgd->rd_length +
  501. 2 * RES_DINODE +
  502. RES_STATFS + RES_QUOTA, 0);
  503. if (error)
  504. goto fail_ipreserv;
  505. } else {
  506. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  507. if (error)
  508. goto fail_quota_locks;
  509. }
  510. error = gfs2_dir_add(&dip->i_inode, name, ip);
  511. if (error)
  512. goto fail_end_trans;
  513. error = gfs2_meta_inode_buffer(ip, &dibh);
  514. if (error)
  515. goto fail_end_trans;
  516. inc_nlink(&ip->i_inode);
  517. if (S_ISDIR(ip->i_inode.i_mode))
  518. inc_nlink(&ip->i_inode);
  519. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  520. gfs2_dinode_out(ip, dibh->b_data);
  521. brelse(dibh);
  522. return 0;
  523. fail_end_trans:
  524. gfs2_trans_end(sdp);
  525. fail_ipreserv:
  526. if (dip->i_alloc->al_rgd)
  527. gfs2_inplace_release(dip);
  528. fail_quota_locks:
  529. gfs2_quota_unlock(dip);
  530. fail:
  531. gfs2_alloc_put(dip);
  532. return error;
  533. }
  534. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
  535. const struct qstr *qstr)
  536. {
  537. int err;
  538. size_t len;
  539. void *value;
  540. char *name;
  541. err = security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
  542. &name, &value, &len);
  543. if (err) {
  544. if (err == -EOPNOTSUPP)
  545. return 0;
  546. return err;
  547. }
  548. err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
  549. GFS2_EATYPE_SECURITY);
  550. kfree(value);
  551. kfree(name);
  552. return err;
  553. }
  554. /**
  555. * gfs2_create_inode - Create a new inode
  556. * @dir: The parent directory
  557. * @dentry: The new dentry
  558. * @mode: The permissions on the new inode
  559. * @dev: For device nodes, this is the device number
  560. * @symname: For symlinks, this is the link destination
  561. * @size: The initial size of the inode (ignored for directories)
  562. *
  563. * Returns: 0 on success, or error code
  564. */
  565. static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  566. unsigned int mode, dev_t dev, const char *symname,
  567. unsigned int size)
  568. {
  569. const struct qstr *name = &dentry->d_name;
  570. struct gfs2_holder ghs[2];
  571. struct inode *inode = NULL;
  572. struct gfs2_inode *dip = GFS2_I(dir);
  573. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  574. struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
  575. int error;
  576. u64 generation;
  577. struct buffer_head *bh = NULL;
  578. if (!name->len || name->len > GFS2_FNAMESIZE)
  579. return -ENAMETOOLONG;
  580. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  581. if (error)
  582. goto fail;
  583. error = create_ok(dip, name, mode);
  584. if (error)
  585. goto fail_gunlock;
  586. error = alloc_dinode(dip, &inum.no_addr, &generation);
  587. if (error)
  588. goto fail_gunlock;
  589. inum.no_formal_ino = generation;
  590. error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
  591. LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  592. if (error)
  593. goto fail_gunlock;
  594. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, symname, size, &bh);
  595. if (error)
  596. goto fail_gunlock2;
  597. inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
  598. inum.no_formal_ino, 0);
  599. if (IS_ERR(inode))
  600. goto fail_gunlock2;
  601. error = gfs2_inode_refresh(GFS2_I(inode));
  602. if (error)
  603. goto fail_gunlock2;
  604. error = gfs2_acl_create(dip, inode);
  605. if (error)
  606. goto fail_gunlock2;
  607. error = gfs2_security_init(dip, GFS2_I(inode), name);
  608. if (error)
  609. goto fail_gunlock2;
  610. error = link_dinode(dip, name, GFS2_I(inode));
  611. if (error)
  612. goto fail_gunlock2;
  613. if (bh)
  614. brelse(bh);
  615. gfs2_trans_end(sdp);
  616. if (dip->i_alloc->al_rgd)
  617. gfs2_inplace_release(dip);
  618. gfs2_quota_unlock(dip);
  619. gfs2_alloc_put(dip);
  620. gfs2_glock_dq_uninit_m(2, ghs);
  621. mark_inode_dirty(inode);
  622. d_instantiate(dentry, inode);
  623. return 0;
  624. fail_gunlock2:
  625. gfs2_glock_dq_uninit(ghs + 1);
  626. if (inode && !IS_ERR(inode))
  627. iput(inode);
  628. fail_gunlock:
  629. gfs2_glock_dq_uninit(ghs);
  630. fail:
  631. if (bh)
  632. brelse(bh);
  633. return error;
  634. }
  635. /**
  636. * gfs2_create - Create a file
  637. * @dir: The directory in which to create the file
  638. * @dentry: The dentry of the new file
  639. * @mode: The mode of the new file
  640. *
  641. * Returns: errno
  642. */
  643. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  644. int mode, struct nameidata *nd)
  645. {
  646. struct inode *inode;
  647. int ret;
  648. for (;;) {
  649. ret = gfs2_create_inode(dir, dentry, S_IFREG | mode, 0, NULL, 0);
  650. if (ret != -EEXIST || (nd && (nd->flags & LOOKUP_EXCL)))
  651. return ret;
  652. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  653. if (inode) {
  654. if (!IS_ERR(inode))
  655. break;
  656. return PTR_ERR(inode);
  657. }
  658. }
  659. d_instantiate(dentry, inode);
  660. return 0;
  661. }
  662. /**
  663. * gfs2_lookup - Look up a filename in a directory and return its inode
  664. * @dir: The directory inode
  665. * @dentry: The dentry of the new inode
  666. * @nd: passed from Linux VFS, ignored by us
  667. *
  668. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  669. *
  670. * Returns: errno
  671. */
  672. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  673. struct nameidata *nd)
  674. {
  675. struct inode *inode = NULL;
  676. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  677. if (inode && IS_ERR(inode))
  678. return ERR_CAST(inode);
  679. if (inode) {
  680. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  681. struct gfs2_holder gh;
  682. int error;
  683. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  684. if (error) {
  685. iput(inode);
  686. return ERR_PTR(error);
  687. }
  688. gfs2_glock_dq_uninit(&gh);
  689. return d_splice_alias(inode, dentry);
  690. }
  691. d_add(dentry, inode);
  692. return NULL;
  693. }
  694. /**
  695. * gfs2_link - Link to a file
  696. * @old_dentry: The inode to link
  697. * @dir: Add link to this directory
  698. * @dentry: The name of the link
  699. *
  700. * Link the inode in "old_dentry" into the directory "dir" with the
  701. * name in "dentry".
  702. *
  703. * Returns: errno
  704. */
  705. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  706. struct dentry *dentry)
  707. {
  708. struct gfs2_inode *dip = GFS2_I(dir);
  709. struct gfs2_sbd *sdp = GFS2_SB(dir);
  710. struct inode *inode = old_dentry->d_inode;
  711. struct gfs2_inode *ip = GFS2_I(inode);
  712. struct gfs2_holder ghs[2];
  713. struct buffer_head *dibh;
  714. int alloc_required;
  715. int error;
  716. if (S_ISDIR(inode->i_mode))
  717. return -EPERM;
  718. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  719. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  720. error = gfs2_glock_nq(ghs); /* parent */
  721. if (error)
  722. goto out_parent;
  723. error = gfs2_glock_nq(ghs + 1); /* child */
  724. if (error)
  725. goto out_child;
  726. error = -ENOENT;
  727. if (inode->i_nlink == 0)
  728. goto out_gunlock;
  729. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC, 0);
  730. if (error)
  731. goto out_gunlock;
  732. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  733. switch (error) {
  734. case -ENOENT:
  735. break;
  736. case 0:
  737. error = -EEXIST;
  738. default:
  739. goto out_gunlock;
  740. }
  741. error = -EINVAL;
  742. if (!dip->i_inode.i_nlink)
  743. goto out_gunlock;
  744. error = -EFBIG;
  745. if (dip->i_entries == (u32)-1)
  746. goto out_gunlock;
  747. error = -EPERM;
  748. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  749. goto out_gunlock;
  750. error = -EINVAL;
  751. if (!ip->i_inode.i_nlink)
  752. goto out_gunlock;
  753. error = -EMLINK;
  754. if (ip->i_inode.i_nlink == (u32)-1)
  755. goto out_gunlock;
  756. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  757. if (error < 0)
  758. goto out_gunlock;
  759. error = 0;
  760. if (alloc_required) {
  761. struct gfs2_alloc *al = gfs2_alloc_get(dip);
  762. if (!al) {
  763. error = -ENOMEM;
  764. goto out_gunlock;
  765. }
  766. error = gfs2_quota_lock_check(dip);
  767. if (error)
  768. goto out_alloc;
  769. al->al_requested = sdp->sd_max_dirres;
  770. error = gfs2_inplace_reserve(dip);
  771. if (error)
  772. goto out_gunlock_q;
  773. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  774. gfs2_rg_blocks(al) +
  775. 2 * RES_DINODE + RES_STATFS +
  776. RES_QUOTA, 0);
  777. if (error)
  778. goto out_ipres;
  779. } else {
  780. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  781. if (error)
  782. goto out_ipres;
  783. }
  784. error = gfs2_meta_inode_buffer(ip, &dibh);
  785. if (error)
  786. goto out_end_trans;
  787. error = gfs2_dir_add(dir, &dentry->d_name, ip);
  788. if (error)
  789. goto out_brelse;
  790. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  791. inc_nlink(&ip->i_inode);
  792. ip->i_inode.i_ctime = CURRENT_TIME;
  793. gfs2_dinode_out(ip, dibh->b_data);
  794. mark_inode_dirty(&ip->i_inode);
  795. out_brelse:
  796. brelse(dibh);
  797. out_end_trans:
  798. gfs2_trans_end(sdp);
  799. out_ipres:
  800. if (alloc_required)
  801. gfs2_inplace_release(dip);
  802. out_gunlock_q:
  803. if (alloc_required)
  804. gfs2_quota_unlock(dip);
  805. out_alloc:
  806. if (alloc_required)
  807. gfs2_alloc_put(dip);
  808. out_gunlock:
  809. gfs2_glock_dq(ghs + 1);
  810. out_child:
  811. gfs2_glock_dq(ghs);
  812. out_parent:
  813. gfs2_holder_uninit(ghs);
  814. gfs2_holder_uninit(ghs + 1);
  815. if (!error) {
  816. ihold(inode);
  817. d_instantiate(dentry, inode);
  818. mark_inode_dirty(inode);
  819. }
  820. return error;
  821. }
  822. /*
  823. * gfs2_unlink_ok - check to see that a inode is still in a directory
  824. * @dip: the directory
  825. * @name: the name of the file
  826. * @ip: the inode
  827. *
  828. * Assumes that the lock on (at least) @dip is held.
  829. *
  830. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  831. */
  832. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  833. const struct gfs2_inode *ip)
  834. {
  835. int error;
  836. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  837. return -EPERM;
  838. if ((dip->i_inode.i_mode & S_ISVTX) &&
  839. dip->i_inode.i_uid != current_fsuid() &&
  840. ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
  841. return -EPERM;
  842. if (IS_APPEND(&dip->i_inode))
  843. return -EPERM;
  844. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
  845. if (error)
  846. return error;
  847. error = gfs2_dir_check(&dip->i_inode, name, ip);
  848. if (error)
  849. return error;
  850. return 0;
  851. }
  852. /**
  853. * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
  854. * @dip: The parent directory
  855. * @name: The name of the entry in the parent directory
  856. * @bh: The inode buffer for the inode to be removed
  857. * @inode: The inode to be removed
  858. *
  859. * Called with all the locks and in a transaction. This will only be
  860. * called for a directory after it has been checked to ensure it is empty.
  861. *
  862. * Returns: 0 on success, or an error
  863. */
  864. static int gfs2_unlink_inode(struct gfs2_inode *dip,
  865. const struct dentry *dentry,
  866. struct buffer_head *bh)
  867. {
  868. struct inode *inode = dentry->d_inode;
  869. struct gfs2_inode *ip = GFS2_I(inode);
  870. int error;
  871. error = gfs2_dir_del(dip, dentry);
  872. if (error)
  873. return error;
  874. ip->i_entries = 0;
  875. inode->i_ctime = CURRENT_TIME;
  876. if (S_ISDIR(inode->i_mode))
  877. clear_nlink(inode);
  878. else
  879. drop_nlink(inode);
  880. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  881. gfs2_dinode_out(ip, bh->b_data);
  882. mark_inode_dirty(inode);
  883. if (inode->i_nlink == 0)
  884. gfs2_unlink_di(inode);
  885. return 0;
  886. }
  887. /**
  888. * gfs2_unlink - Unlink an inode (this does rmdir as well)
  889. * @dir: The inode of the directory containing the inode to unlink
  890. * @dentry: The file itself
  891. *
  892. * This routine uses the type of the inode as a flag to figure out
  893. * whether this is an unlink or an rmdir.
  894. *
  895. * Returns: errno
  896. */
  897. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  898. {
  899. struct gfs2_inode *dip = GFS2_I(dir);
  900. struct gfs2_sbd *sdp = GFS2_SB(dir);
  901. struct inode *inode = dentry->d_inode;
  902. struct gfs2_inode *ip = GFS2_I(inode);
  903. struct buffer_head *bh;
  904. struct gfs2_holder ghs[3];
  905. struct gfs2_rgrpd *rgd;
  906. struct gfs2_holder ri_gh;
  907. int error;
  908. error = gfs2_rindex_hold(sdp, &ri_gh);
  909. if (error)
  910. return error;
  911. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  912. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  913. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  914. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  915. error = gfs2_glock_nq(ghs); /* parent */
  916. if (error)
  917. goto out_parent;
  918. error = gfs2_glock_nq(ghs + 1); /* child */
  919. if (error)
  920. goto out_child;
  921. error = -ENOENT;
  922. if (inode->i_nlink == 0)
  923. goto out_rgrp;
  924. if (S_ISDIR(inode->i_mode)) {
  925. error = -ENOTEMPTY;
  926. if (ip->i_entries > 2 || inode->i_nlink > 2)
  927. goto out_rgrp;
  928. }
  929. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  930. if (error)
  931. goto out_rgrp;
  932. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  933. if (error)
  934. goto out_gunlock;
  935. error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
  936. if (error)
  937. goto out_gunlock;
  938. error = gfs2_meta_inode_buffer(ip, &bh);
  939. if (error)
  940. goto out_end_trans;
  941. error = gfs2_unlink_inode(dip, dentry, bh);
  942. brelse(bh);
  943. out_end_trans:
  944. gfs2_trans_end(sdp);
  945. out_gunlock:
  946. gfs2_glock_dq(ghs + 2);
  947. out_rgrp:
  948. gfs2_holder_uninit(ghs + 2);
  949. gfs2_glock_dq(ghs + 1);
  950. out_child:
  951. gfs2_holder_uninit(ghs + 1);
  952. gfs2_glock_dq(ghs);
  953. out_parent:
  954. gfs2_holder_uninit(ghs);
  955. gfs2_glock_dq_uninit(&ri_gh);
  956. return error;
  957. }
  958. /**
  959. * gfs2_symlink - Create a symlink
  960. * @dir: The directory to create the symlink in
  961. * @dentry: The dentry to put the symlink in
  962. * @symname: The thing which the link points to
  963. *
  964. * Returns: errno
  965. */
  966. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  967. const char *symname)
  968. {
  969. struct gfs2_sbd *sdp = GFS2_SB(dir);
  970. unsigned int size;
  971. size = strlen(symname);
  972. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  973. return -ENAMETOOLONG;
  974. return gfs2_create_inode(dir, dentry, S_IFLNK | S_IRWXUGO, 0, symname, size);
  975. }
  976. /**
  977. * gfs2_mkdir - Make a directory
  978. * @dir: The parent directory of the new one
  979. * @dentry: The dentry of the new directory
  980. * @mode: The mode of the new directory
  981. *
  982. * Returns: errno
  983. */
  984. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  985. {
  986. return gfs2_create_inode(dir, dentry, S_IFDIR | mode, 0, NULL, 0);
  987. }
  988. /**
  989. * gfs2_mknod - Make a special file
  990. * @dir: The directory in which the special file will reside
  991. * @dentry: The dentry of the special file
  992. * @mode: The mode of the special file
  993. * @dev: The device specification of the special file
  994. *
  995. */
  996. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  997. dev_t dev)
  998. {
  999. return gfs2_create_inode(dir, dentry, mode, dev, NULL, 0);
  1000. }
  1001. /*
  1002. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  1003. * @this: move this
  1004. * @to: to here
  1005. *
  1006. * Follow @to back to the root and make sure we don't encounter @this
  1007. * Assumes we already hold the rename lock.
  1008. *
  1009. * Returns: errno
  1010. */
  1011. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  1012. {
  1013. struct inode *dir = &to->i_inode;
  1014. struct super_block *sb = dir->i_sb;
  1015. struct inode *tmp;
  1016. int error = 0;
  1017. igrab(dir);
  1018. for (;;) {
  1019. if (dir == &this->i_inode) {
  1020. error = -EINVAL;
  1021. break;
  1022. }
  1023. if (dir == sb->s_root->d_inode) {
  1024. error = 0;
  1025. break;
  1026. }
  1027. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  1028. if (IS_ERR(tmp)) {
  1029. error = PTR_ERR(tmp);
  1030. break;
  1031. }
  1032. iput(dir);
  1033. dir = tmp;
  1034. }
  1035. iput(dir);
  1036. return error;
  1037. }
  1038. /**
  1039. * gfs2_rename - Rename a file
  1040. * @odir: Parent directory of old file name
  1041. * @odentry: The old dentry of the file
  1042. * @ndir: Parent directory of new file name
  1043. * @ndentry: The new dentry of the file
  1044. *
  1045. * Returns: errno
  1046. */
  1047. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  1048. struct inode *ndir, struct dentry *ndentry)
  1049. {
  1050. struct gfs2_inode *odip = GFS2_I(odir);
  1051. struct gfs2_inode *ndip = GFS2_I(ndir);
  1052. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  1053. struct gfs2_inode *nip = NULL;
  1054. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1055. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh;
  1056. struct gfs2_rgrpd *nrgd;
  1057. unsigned int num_gh;
  1058. int dir_rename = 0;
  1059. int alloc_required = 0;
  1060. unsigned int x;
  1061. int error;
  1062. if (ndentry->d_inode) {
  1063. nip = GFS2_I(ndentry->d_inode);
  1064. if (ip == nip)
  1065. return 0;
  1066. }
  1067. error = gfs2_rindex_hold(sdp, &ri_gh);
  1068. if (error)
  1069. return error;
  1070. if (odip != ndip) {
  1071. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1072. 0, &r_gh);
  1073. if (error)
  1074. goto out;
  1075. if (S_ISDIR(ip->i_inode.i_mode)) {
  1076. dir_rename = 1;
  1077. /* don't move a dirctory into it's subdir */
  1078. error = gfs2_ok_to_move(ip, ndip);
  1079. if (error)
  1080. goto out_gunlock_r;
  1081. }
  1082. }
  1083. num_gh = 1;
  1084. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1085. if (odip != ndip) {
  1086. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1087. num_gh++;
  1088. }
  1089. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1090. num_gh++;
  1091. if (nip) {
  1092. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1093. num_gh++;
  1094. /* grab the resource lock for unlink flag twiddling
  1095. * this is the case of the target file already existing
  1096. * so we unlink before doing the rename
  1097. */
  1098. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  1099. if (nrgd)
  1100. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  1101. }
  1102. for (x = 0; x < num_gh; x++) {
  1103. error = gfs2_glock_nq(ghs + x);
  1104. if (error)
  1105. goto out_gunlock;
  1106. }
  1107. error = -ENOENT;
  1108. if (ip->i_inode.i_nlink == 0)
  1109. goto out_gunlock;
  1110. /* Check out the old directory */
  1111. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  1112. if (error)
  1113. goto out_gunlock;
  1114. /* Check out the new directory */
  1115. if (nip) {
  1116. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1117. if (error)
  1118. goto out_gunlock;
  1119. if (nip->i_inode.i_nlink == 0) {
  1120. error = -EAGAIN;
  1121. goto out_gunlock;
  1122. }
  1123. if (S_ISDIR(nip->i_inode.i_mode)) {
  1124. if (nip->i_entries < 2) {
  1125. gfs2_consist_inode(nip);
  1126. error = -EIO;
  1127. goto out_gunlock;
  1128. }
  1129. if (nip->i_entries > 2) {
  1130. error = -ENOTEMPTY;
  1131. goto out_gunlock;
  1132. }
  1133. }
  1134. } else {
  1135. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC, 0);
  1136. if (error)
  1137. goto out_gunlock;
  1138. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  1139. switch (error) {
  1140. case -ENOENT:
  1141. error = 0;
  1142. break;
  1143. case 0:
  1144. error = -EEXIST;
  1145. default:
  1146. goto out_gunlock;
  1147. };
  1148. if (odip != ndip) {
  1149. if (!ndip->i_inode.i_nlink) {
  1150. error = -ENOENT;
  1151. goto out_gunlock;
  1152. }
  1153. if (ndip->i_entries == (u32)-1) {
  1154. error = -EFBIG;
  1155. goto out_gunlock;
  1156. }
  1157. if (S_ISDIR(ip->i_inode.i_mode) &&
  1158. ndip->i_inode.i_nlink == (u32)-1) {
  1159. error = -EMLINK;
  1160. goto out_gunlock;
  1161. }
  1162. }
  1163. }
  1164. /* Check out the dir to be renamed */
  1165. if (dir_rename) {
  1166. error = gfs2_permission(odentry->d_inode, MAY_WRITE, 0);
  1167. if (error)
  1168. goto out_gunlock;
  1169. }
  1170. if (nip == NULL)
  1171. alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  1172. error = alloc_required;
  1173. if (error < 0)
  1174. goto out_gunlock;
  1175. error = 0;
  1176. if (alloc_required) {
  1177. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  1178. if (!al) {
  1179. error = -ENOMEM;
  1180. goto out_gunlock;
  1181. }
  1182. error = gfs2_quota_lock_check(ndip);
  1183. if (error)
  1184. goto out_alloc;
  1185. al->al_requested = sdp->sd_max_dirres;
  1186. error = gfs2_inplace_reserve_ri(ndip);
  1187. if (error)
  1188. goto out_gunlock_q;
  1189. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  1190. gfs2_rg_blocks(al) +
  1191. 4 * RES_DINODE + 4 * RES_LEAF +
  1192. RES_STATFS + RES_QUOTA + 4, 0);
  1193. if (error)
  1194. goto out_ipreserv;
  1195. } else {
  1196. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  1197. 5 * RES_LEAF + 4, 0);
  1198. if (error)
  1199. goto out_gunlock;
  1200. }
  1201. /* Remove the target file, if it exists */
  1202. if (nip) {
  1203. struct buffer_head *bh;
  1204. error = gfs2_meta_inode_buffer(nip, &bh);
  1205. if (error)
  1206. goto out_end_trans;
  1207. error = gfs2_unlink_inode(ndip, ndentry, bh);
  1208. brelse(bh);
  1209. }
  1210. if (dir_rename) {
  1211. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  1212. if (error)
  1213. goto out_end_trans;
  1214. } else {
  1215. struct buffer_head *dibh;
  1216. error = gfs2_meta_inode_buffer(ip, &dibh);
  1217. if (error)
  1218. goto out_end_trans;
  1219. ip->i_inode.i_ctime = CURRENT_TIME;
  1220. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1221. gfs2_dinode_out(ip, dibh->b_data);
  1222. brelse(dibh);
  1223. }
  1224. error = gfs2_dir_del(odip, odentry);
  1225. if (error)
  1226. goto out_end_trans;
  1227. error = gfs2_dir_add(ndir, &ndentry->d_name, ip);
  1228. if (error)
  1229. goto out_end_trans;
  1230. out_end_trans:
  1231. gfs2_trans_end(sdp);
  1232. out_ipreserv:
  1233. if (alloc_required)
  1234. gfs2_inplace_release(ndip);
  1235. out_gunlock_q:
  1236. if (alloc_required)
  1237. gfs2_quota_unlock(ndip);
  1238. out_alloc:
  1239. if (alloc_required)
  1240. gfs2_alloc_put(ndip);
  1241. out_gunlock:
  1242. while (x--) {
  1243. gfs2_glock_dq(ghs + x);
  1244. gfs2_holder_uninit(ghs + x);
  1245. }
  1246. out_gunlock_r:
  1247. if (r_gh.gh_gl)
  1248. gfs2_glock_dq_uninit(&r_gh);
  1249. out:
  1250. gfs2_glock_dq_uninit(&ri_gh);
  1251. return error;
  1252. }
  1253. /**
  1254. * gfs2_follow_link - Follow a symbolic link
  1255. * @dentry: The dentry of the link
  1256. * @nd: Data that we pass to vfs_follow_link()
  1257. *
  1258. * This can handle symlinks of any size.
  1259. *
  1260. * Returns: 0 on success or error code
  1261. */
  1262. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  1263. {
  1264. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  1265. struct gfs2_holder i_gh;
  1266. struct buffer_head *dibh;
  1267. unsigned int size;
  1268. char *buf;
  1269. int error;
  1270. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  1271. error = gfs2_glock_nq(&i_gh);
  1272. if (error) {
  1273. gfs2_holder_uninit(&i_gh);
  1274. nd_set_link(nd, ERR_PTR(error));
  1275. return NULL;
  1276. }
  1277. size = (unsigned int)i_size_read(&ip->i_inode);
  1278. if (size == 0) {
  1279. gfs2_consist_inode(ip);
  1280. buf = ERR_PTR(-EIO);
  1281. goto out;
  1282. }
  1283. error = gfs2_meta_inode_buffer(ip, &dibh);
  1284. if (error) {
  1285. buf = ERR_PTR(error);
  1286. goto out;
  1287. }
  1288. buf = kzalloc(size + 1, GFP_NOFS);
  1289. if (!buf)
  1290. buf = ERR_PTR(-ENOMEM);
  1291. else
  1292. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  1293. brelse(dibh);
  1294. out:
  1295. gfs2_glock_dq_uninit(&i_gh);
  1296. nd_set_link(nd, buf);
  1297. return NULL;
  1298. }
  1299. static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  1300. {
  1301. char *s = nd_get_link(nd);
  1302. if (!IS_ERR(s))
  1303. kfree(s);
  1304. }
  1305. /**
  1306. * gfs2_permission -
  1307. * @inode: The inode
  1308. * @mask: The mask to be tested
  1309. * @flags: Indicates whether this is an RCU path walk or not
  1310. *
  1311. * This may be called from the VFS directly, or from within GFS2 with the
  1312. * inode locked, so we look to see if the glock is already locked and only
  1313. * lock the glock if its not already been done.
  1314. *
  1315. * Returns: errno
  1316. */
  1317. int gfs2_permission(struct inode *inode, int mask, unsigned int flags)
  1318. {
  1319. struct gfs2_inode *ip;
  1320. struct gfs2_holder i_gh;
  1321. int error;
  1322. int unlock = 0;
  1323. ip = GFS2_I(inode);
  1324. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1325. if (flags & IPERM_FLAG_RCU)
  1326. return -ECHILD;
  1327. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  1328. if (error)
  1329. return error;
  1330. unlock = 1;
  1331. }
  1332. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  1333. error = -EACCES;
  1334. else
  1335. error = generic_permission(inode, mask, flags, gfs2_check_acl);
  1336. if (unlock)
  1337. gfs2_glock_dq_uninit(&i_gh);
  1338. return error;
  1339. }
  1340. static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1341. {
  1342. struct inode *inode = &ip->i_inode;
  1343. struct buffer_head *dibh;
  1344. int error;
  1345. error = gfs2_meta_inode_buffer(ip, &dibh);
  1346. if (error)
  1347. return error;
  1348. setattr_copy(inode, attr);
  1349. mark_inode_dirty(inode);
  1350. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1351. gfs2_dinode_out(ip, dibh->b_data);
  1352. brelse(dibh);
  1353. return 0;
  1354. }
  1355. /**
  1356. * gfs2_setattr_simple -
  1357. * @ip:
  1358. * @attr:
  1359. *
  1360. * Returns: errno
  1361. */
  1362. int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1363. {
  1364. int error;
  1365. if (current->journal_info)
  1366. return __gfs2_setattr_simple(ip, attr);
  1367. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
  1368. if (error)
  1369. return error;
  1370. error = __gfs2_setattr_simple(ip, attr);
  1371. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  1372. return error;
  1373. }
  1374. static int setattr_chown(struct inode *inode, struct iattr *attr)
  1375. {
  1376. struct gfs2_inode *ip = GFS2_I(inode);
  1377. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1378. u32 ouid, ogid, nuid, ngid;
  1379. int error;
  1380. ouid = inode->i_uid;
  1381. ogid = inode->i_gid;
  1382. nuid = attr->ia_uid;
  1383. ngid = attr->ia_gid;
  1384. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  1385. ouid = nuid = NO_QUOTA_CHANGE;
  1386. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  1387. ogid = ngid = NO_QUOTA_CHANGE;
  1388. if (!gfs2_alloc_get(ip))
  1389. return -ENOMEM;
  1390. error = gfs2_quota_lock(ip, nuid, ngid);
  1391. if (error)
  1392. goto out_alloc;
  1393. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1394. error = gfs2_quota_check(ip, nuid, ngid);
  1395. if (error)
  1396. goto out_gunlock_q;
  1397. }
  1398. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  1399. if (error)
  1400. goto out_gunlock_q;
  1401. error = gfs2_setattr_simple(ip, attr);
  1402. if (error)
  1403. goto out_end_trans;
  1404. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1405. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  1406. gfs2_quota_change(ip, -blocks, ouid, ogid);
  1407. gfs2_quota_change(ip, blocks, nuid, ngid);
  1408. }
  1409. out_end_trans:
  1410. gfs2_trans_end(sdp);
  1411. out_gunlock_q:
  1412. gfs2_quota_unlock(ip);
  1413. out_alloc:
  1414. gfs2_alloc_put(ip);
  1415. return error;
  1416. }
  1417. /**
  1418. * gfs2_setattr - Change attributes on an inode
  1419. * @dentry: The dentry which is changing
  1420. * @attr: The structure describing the change
  1421. *
  1422. * The VFS layer wants to change one or more of an inodes attributes. Write
  1423. * that change out to disk.
  1424. *
  1425. * Returns: errno
  1426. */
  1427. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1428. {
  1429. struct inode *inode = dentry->d_inode;
  1430. struct gfs2_inode *ip = GFS2_I(inode);
  1431. struct gfs2_holder i_gh;
  1432. int error;
  1433. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1434. if (error)
  1435. return error;
  1436. error = -EPERM;
  1437. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1438. goto out;
  1439. error = inode_change_ok(inode, attr);
  1440. if (error)
  1441. goto out;
  1442. if (attr->ia_valid & ATTR_SIZE)
  1443. error = gfs2_setattr_size(inode, attr->ia_size);
  1444. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1445. error = setattr_chown(inode, attr);
  1446. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  1447. error = gfs2_acl_chmod(ip, attr);
  1448. else
  1449. error = gfs2_setattr_simple(ip, attr);
  1450. out:
  1451. gfs2_glock_dq_uninit(&i_gh);
  1452. if (!error)
  1453. mark_inode_dirty(inode);
  1454. return error;
  1455. }
  1456. /**
  1457. * gfs2_getattr - Read out an inode's attributes
  1458. * @mnt: The vfsmount the inode is being accessed from
  1459. * @dentry: The dentry to stat
  1460. * @stat: The inode's stats
  1461. *
  1462. * This may be called from the VFS directly, or from within GFS2 with the
  1463. * inode locked, so we look to see if the glock is already locked and only
  1464. * lock the glock if its not already been done. Note that its the NFS
  1465. * readdirplus operation which causes this to be called (from filldir)
  1466. * with the glock already held.
  1467. *
  1468. * Returns: errno
  1469. */
  1470. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1471. struct kstat *stat)
  1472. {
  1473. struct inode *inode = dentry->d_inode;
  1474. struct gfs2_inode *ip = GFS2_I(inode);
  1475. struct gfs2_holder gh;
  1476. int error;
  1477. int unlock = 0;
  1478. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1479. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1480. if (error)
  1481. return error;
  1482. unlock = 1;
  1483. }
  1484. generic_fillattr(inode, stat);
  1485. if (unlock)
  1486. gfs2_glock_dq_uninit(&gh);
  1487. return 0;
  1488. }
  1489. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1490. const void *data, size_t size, int flags)
  1491. {
  1492. struct inode *inode = dentry->d_inode;
  1493. struct gfs2_inode *ip = GFS2_I(inode);
  1494. struct gfs2_holder gh;
  1495. int ret;
  1496. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1497. ret = gfs2_glock_nq(&gh);
  1498. if (ret == 0) {
  1499. ret = generic_setxattr(dentry, name, data, size, flags);
  1500. gfs2_glock_dq(&gh);
  1501. }
  1502. gfs2_holder_uninit(&gh);
  1503. return ret;
  1504. }
  1505. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1506. void *data, size_t size)
  1507. {
  1508. struct inode *inode = dentry->d_inode;
  1509. struct gfs2_inode *ip = GFS2_I(inode);
  1510. struct gfs2_holder gh;
  1511. int ret;
  1512. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1513. ret = gfs2_glock_nq(&gh);
  1514. if (ret == 0) {
  1515. ret = generic_getxattr(dentry, name, data, size);
  1516. gfs2_glock_dq(&gh);
  1517. }
  1518. gfs2_holder_uninit(&gh);
  1519. return ret;
  1520. }
  1521. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1522. {
  1523. struct inode *inode = dentry->d_inode;
  1524. struct gfs2_inode *ip = GFS2_I(inode);
  1525. struct gfs2_holder gh;
  1526. int ret;
  1527. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1528. ret = gfs2_glock_nq(&gh);
  1529. if (ret == 0) {
  1530. ret = generic_removexattr(dentry, name);
  1531. gfs2_glock_dq(&gh);
  1532. }
  1533. gfs2_holder_uninit(&gh);
  1534. return ret;
  1535. }
  1536. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1537. u64 start, u64 len)
  1538. {
  1539. struct gfs2_inode *ip = GFS2_I(inode);
  1540. struct gfs2_holder gh;
  1541. int ret;
  1542. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1543. if (ret)
  1544. return ret;
  1545. mutex_lock(&inode->i_mutex);
  1546. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1547. if (ret)
  1548. goto out;
  1549. if (gfs2_is_stuffed(ip)) {
  1550. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1551. u64 size = i_size_read(inode);
  1552. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1553. FIEMAP_EXTENT_DATA_INLINE;
  1554. phys += sizeof(struct gfs2_dinode);
  1555. phys += start;
  1556. if (start + len > size)
  1557. len = size - start;
  1558. if (start < size)
  1559. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1560. len, flags);
  1561. if (ret == 1)
  1562. ret = 0;
  1563. } else {
  1564. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1565. gfs2_block_map);
  1566. }
  1567. gfs2_glock_dq_uninit(&gh);
  1568. out:
  1569. mutex_unlock(&inode->i_mutex);
  1570. return ret;
  1571. }
  1572. const struct inode_operations gfs2_file_iops = {
  1573. .permission = gfs2_permission,
  1574. .setattr = gfs2_setattr,
  1575. .getattr = gfs2_getattr,
  1576. .setxattr = gfs2_setxattr,
  1577. .getxattr = gfs2_getxattr,
  1578. .listxattr = gfs2_listxattr,
  1579. .removexattr = gfs2_removexattr,
  1580. .fiemap = gfs2_fiemap,
  1581. };
  1582. const struct inode_operations gfs2_dir_iops = {
  1583. .create = gfs2_create,
  1584. .lookup = gfs2_lookup,
  1585. .link = gfs2_link,
  1586. .unlink = gfs2_unlink,
  1587. .symlink = gfs2_symlink,
  1588. .mkdir = gfs2_mkdir,
  1589. .rmdir = gfs2_unlink,
  1590. .mknod = gfs2_mknod,
  1591. .rename = gfs2_rename,
  1592. .permission = gfs2_permission,
  1593. .setattr = gfs2_setattr,
  1594. .getattr = gfs2_getattr,
  1595. .setxattr = gfs2_setxattr,
  1596. .getxattr = gfs2_getxattr,
  1597. .listxattr = gfs2_listxattr,
  1598. .removexattr = gfs2_removexattr,
  1599. .fiemap = gfs2_fiemap,
  1600. };
  1601. const struct inode_operations gfs2_symlink_iops = {
  1602. .readlink = generic_readlink,
  1603. .follow_link = gfs2_follow_link,
  1604. .put_link = gfs2_put_link,
  1605. .permission = gfs2_permission,
  1606. .setattr = gfs2_setattr,
  1607. .getattr = gfs2_getattr,
  1608. .setxattr = gfs2_setxattr,
  1609. .getxattr = gfs2_getxattr,
  1610. .listxattr = gfs2_listxattr,
  1611. .removexattr = gfs2_removexattr,
  1612. .fiemap = gfs2_fiemap,
  1613. };